I have successfully connected to a remote server through a proxy using HttpWebRequest with the Method property set to "CONNECT".However it only seems possible to read data from the stream once connected, making the connection useless since I can't interact with the server that was the target of the CONNECT.
Sample code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://remote.server.name:999");
request.Method = WebRequestMethods.Http.Connect;
request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
byte[] input = new byte[10240];
stream.Read(input, 0, input.Length);
byte[] output = GetData();
// I would like to be able to do the following, but a NotSupportedException is thrown (Message: "The stream does not support writing.")
stream.Write(output, 0, output.Length);
Looking at the data in "input" (from the snippet above), the connection to the remote server (i.e. tunnelled through the proxy using CONNECT), however at this point I ought to be able to access a 2-way data-stream, butthe response stream is read only so there's no way to continue communication with the remote server.
The advantage of using HttpWebRequest is that it takes care of NTLM proxy authentication required for the particular proxy I'm connecting through which would be time consuming to re-implement using plain TCP sockets.
Is there any way to get a 2-way connection in this situation?