使用HTTP代理的套接字HTTP

本文关键字:HTTP 套接字 代理 使用 | 更新日期: 2023-09-27 18:15:48

我必须使用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
还是我误解了使用代理的过程?

使用HTTP代理的套接字HTTP

克莱门斯是正确的。

我不需要做一个"CONNECT",我只需要发出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);
string connectMsg = "CONNECT whatismyipaddress.com:80 HTTP/1.1'r'n" +
                "Proxy-Authorization: Basic " + base64ProxyCredentials + "'r'n" +