如何将代理与 TcpClient.ConnectAsync() 一起使用? [英] How to use Proxy with TcpClient.ConnectAsync()?
问题描述
.NET 中的 HTTP 代理支持实际上并不支持较低级别的类,如 TcpClient 或 Socket.但我想通过支持CONNECT"命令的 HTTP 代理连接 TCPServer(ip、端口).
HTTP proxy support in .NET does not actually support the lower level classes like TcpClient or Socket. But I want to connect a TCPServer (ip, port) through HTTP proxy that support 'CONNECT' command.
所以我需要做以下步骤:
So I need to do the following steps:
- 连接到代理.
- 发送
CONNECT Host:Port HTTP/1.1
- 发送
- 等待一行响应.如果包含
HTTP/1.X 200
,则连接成功. - 阅读更多响应行,直到收到空行.
- 它通过代理连接到外部世界.可能与代理进行的任何数据交换.
实际上我是在没有代理的情况下这样做的
Actually I do this without proxy
TcpClient _client;
NetworkStream _stream;
public static async Task<bool> ConnectAsync(string hostname, int port)
{
_client = new TcpClient();
await _client.ConnectAsync(hostname, port).ConfigureAwait(false);
_stream = conn._client.GetStream();
..... Do some stuff
// Connexion OK
return true;
}
如何在连接 TcpClient 之前使用代理和凭据?
How can use proxy and credentials before connecting TcpClient?
推荐答案
我找到了一个基于 .NET:通过带有身份验证的 HTTP 代理连接 TcpClient 和 使用TcpClient绕过代理
I find a solution base on .NET: Connecting a TcpClient through an HTTP proxy with authentication and Bypass the proxy using TcpClient
TcpClient _client;
NetworkStream _stream;
public TcpClient ProxyTcpClient(string targetHost, int targetPort, string httpProxyHost, int httpProxyPort, string proxyUserName, string proxyPassword)
{
const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;
Uri proxyUri = new UriBuilder
{
Scheme = Uri.UriSchemeHttp,
Host = httpProxyHost,
Port = httpProxyPort
}.Uri;
Uri targetUri = new UriBuilder
{
Scheme = Uri.UriSchemeHttp,
Host = targetHost,
Port = targetPort
}.Uri;
WebProxy webProxy = new WebProxy(proxyUri, true);
webProxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword);
WebRequest request = WebRequest.Create(targetUri);
request.Proxy = webProxy;
request.Method = "CONNECT";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
Type responseType = responseStream.GetType();
PropertyInfo connectionProperty = responseType.GetProperty("Connection", Flags);
var connection = connectionProperty.GetValue(responseStream, null);
Type connectionType = connection.GetType();
PropertyInfo networkStreamProperty = connectionType.GetProperty("NetworkStream", Flags);
NetworkStream networkStream = (NetworkStream)networkStreamProperty.GetValue(connection, null);
Type nsType = networkStream.GetType();
PropertyInfo socketProperty = nsType.GetProperty("Socket", Flags);
Socket socket = (Socket)socketProperty.GetValue(networkStream, null);
return new TcpClient { Client = socket };
}
public static async Task<bool> ConnectAsync(string hostname, int port)
{
_client = ProxyTcpClient("IPTargetHost", 1234, "IPProxyHost", 5678, "Userproxy", "Userppwd");
_stream = conn._client.GetStream();
..... Do some stuff
// Connexion OK
return true;
}
这篇关于如何将代理与 TcpClient.ConnectAsync() 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!