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 to proxy.
- Send
CONNECT Host:Port HTTP/1.1<CR><LF>
- Send
<CR><LF>
- Wait for a line of response. If it contains
HTTP/1.X 200
, the connection is successful. - Read further lines of response until receive an empty line.
- It's connected to the outside world through a proxy. Any data exchange as posssible with proxy.
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;
}
How can use proxy and credentials before connecting TcpClient?
I find a solution base on .NET: Connecting a TcpClient through an HTTP proxy with authentication and Bypass the proxy using TcpClient