问题
I'm trying to write a local proxy application. I know how proxy applications work in theory.
I've done everything related to handle incoming connections. But the problem is how should I send request which client requested to specified Url. When I try to create a connection with TcpClient
to specified Url and port, it throws following exception :
No such host is known
Edit : I think I should bypass the proxy something like FireFox is doing even system proxy set.
Any idea will be helpful. Thanks in advance.
回答1:
These links might be of help:
http://www.replicator.org/journal/201011170043-net-connecting-a-tcpclient-through-an-http-proxy-with-authentication
http://alandean.blogspot.co.uk/2010/06/routing-tcpclient-http-requests-through.html
回答2:
Based on colinsmith provided links, I've done to bypass proxy using TcpClient. Here is how I did that :
public static TcpClient CreateTcpClient(string url)
{
var webRequest = WebRequest.Create(url);
webRequest.Proxy = null;
var webResponse = webRequest.GetResponse();
var resposeStream = webResponse.GetResponseStream();
const BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
var rsType = resposeStream.GetType();
var connectionProperty = rsType.GetProperty("Connection", flags);
var connection = connectionProperty.GetValue(resposeStream, null);
var connectionType = connection.GetType();
var networkStreamProperty = connectionType.GetProperty("NetworkStream", flags);
var networkStream = networkStreamProperty.GetValue(connection, null);
var nsType = networkStream.GetType();
var socketProperty = nsType.GetProperty("Socket", flags);
var socket = (Socket)socketProperty.GetValue(networkStream, null);
return new TcpClient { Client = socket };
}
Hope this help for others.
来源:https://stackoverflow.com/questions/11710251/bypass-the-proxy-using-tcpclient