|
最近做项目需要用到socket访问http代理服务器,设置头信息有个 Proxy-Authorization 是用来设置代理的,但我设置了取访问代理时,总是获取不到代理的验证
这是我找到的源码,帮我看看哪里有问题
public static Socket ConnectToSocks5Proxy(string proxyAdress, ushort proxyPort, string destAddress, ushort destPort,
string userName, string password)
{
IPAddress destIP = null;
IPAddress proxyIP = null;
byte[] request = new byte[257];
byte[] response = new byte[257];
byte[] tmpBuffer = new byte[40];
proxyIP = IPAddress.Parse(proxyAdress);
IPEndPoint proxyEndPoint = new IPEndPoint(proxyIP, proxyPort);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(proxyEndPoint);
string Proxy_Authorization = string.Format("Basic {0}", Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("adrd11" + ":" + "ad818")));
String temp;
temp = String.Format("CONNECT 127.0.0.1:443 HTTP/1.1\r\nUser-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)\r\n Proxy-Connection: Keep-Alive\r\n Proxy-Authorization: {2}\r\n\r\n", userName, password, Proxy_Authorization);
request = Encoding.ASCII.GetBytes(temp);
s.Send(request, temp.Length, SocketFlags.None);
s.Receive(response); // Get variable length response...
String tempstr = System.Text.Encoding.UTF8.GetString(response);
if (tempstr.Substring(9, 3) == "200")
{
return s;
}
return null;
}
|
|