我必须使用C#的Socket类来加载网页。我今天设法学到了很多,我可以成功创建加载页面所需的GET请求。
但是现在我想使用HTTP代理。
我还学会了如何连接代理。
我现在遇到的问题是,一旦我连接起来,我该如何获得' GET'页面?
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(proxyIP, proxyPort);
string connectMsg = "CONNECT " + proxyIPString + ":" + proxyPortString + " HTTP/1.1\r\n" +
"Proxy-Authorization: Basic " + base64ProxyCredentials + "\r\n" +
"\r\n";
byte[] msg = System.Text.Encoding.ASCII.GetBytes(connectMsg);
s.Send(msg);
string proxyConnectionResponseHeader = "";
while (!proxyConnectionResponseHeader.Contains("\r\n\r\n"))
{
// read the header byte by byte, until \r\n\r\n
byte[] buffer = new byte[1];
s.Receive(buffer, 0, 1, 0);
proxyConnectionResponseHeader += Encoding.ASCII.GetString(buffer);
}
if (!proxyConnectionResponseHeader.Contains("200 Connection established"))
{
return /*some error*/;
}
此时,我已成功连接到代理。为什么以下内容会返回错误请求?
string request = "GET / HTTP/1.1\r\n" +
"Host: whatismyipaddress.com\r\n" +
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
"Accept-Language: en-US,en;q=0.5\r\n" +
"Accept-Encoding: gzip, deflate\r\n" +
"Connection: keep-alive\r\n" +
"\r\n";
msg = Encoding.ASCII.GetBytes(request);
s.Send(msg);
string getResponseHeader = "";
while (!getResponseHeader.Contains("\r\n\r\n"))
{
// read the header byte by byte, until \r\n\r\n
byte[] buffer = new byte[1];
s.Receive(buffer, 0, 1, 0);
getResponseHeader += Encoding.ASCII.GetString(buffer);
}
回复的标题似乎来自代理而不是' whatismyipaddress.com'
HTTP/1.0 400 Bad Request
Server: squid/3.1.19
Mime-Version: 1.0
Date: Sun, 04 Oct 2015 02:00:32 GMT
Content-Type: text/html
Content-Length: 3141
X-Squid-Error: ERR_INVALID_URL 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from funky
X-Cache-Lookup: NONE from funky:<port>
Connection: close
编辑:或者我误解了使用代理的过程?
答案 0 :(得分:1)
克莱门斯是对的。
我不需要做&#34; CONNECT&#34;,我只需要正常发出GET请求,除了通过授权。也使用绝对路径而不是相对路径。
string request = "GET http://whatismyipaddress.com/ HTTP/1.1\r\n" +
"Host: whatismyipaddress.com\r\n" +
"Proxy-Authorization: Basic " + base64ProxyCredentials + "\r\n" +
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
"Accept-Language: en-US,en;q=0.5\r\n" +
"Accept-Encoding: gzip, deflate\r\n" +
"Connection: keep-alive\r\n" +
"\r\n";
msg = Encoding.ASCII.GetBytes(request);
s.Send(msg);