简体   繁体   中英

How to connect to a http proxy via TCP?

I'm trying to figure out how proxy connections work via TCP. I've started by downloading stacksoft library and installing Squid proxy on a remote server.

Now I'm trying to connect to it via c# TcpClient.

The connection is opened by the following request:

CONNECT proxyjudge.iathao.com:80 HTTP/1.1 
HOST proxyjudge.iathao.com

which fails with error code 400 (I've tried several variants of this, it ALWAYS fails):

HTTP/1.0 400 Bad Request
Server: squid/3.1.10
Mime-Version: 1.0
Date: Mon, 27 Aug 2012 13:25:54 GMT
Content-Type: text/html
Content-Length: 3295
X-Squid-Error: ERR_INVALID_REQ 0
Vary: Accept-Language
Content-Language: en
X-Cache: MISS from ****
X-Cache-Lookup: NONE ****:***
Connection: close

....

<h1>ERROR</h1>
<h2>The requested URL could not be retrieved</h2>
....

<div id="content">
<p><b>Invalid Request</b> error was encountered while trying to process the request:</p>

<blockquote id="data">
<pre>CONNECT proxyjudge.iathao.com:80 HTTP/1.1 
HOST proxyjudge.iathao.com

</pre>
</blockquote>

<p>Some possible problems are:</p>
<ul>
<li><p>Missing or unknown request method.</p></li>
<li><p>Missing URL.</p></li>
<li><p>Missing HTTP Identifier (HTTP/1.0).</p></li>
<li><p>Request is too large.</p></li>
<li><p>Content-Length missing for POST or PUT requests.</p></li>
<li><p>Illegal character in hostname; underscores are not allowed.</p></li>
<li><p>HTTP/1.1 <q>Expect:</q> feature is being asked from an HTTP/1.0 software.</p></li>
</ul>

So I've followed requests sent by firefox to the same server with wireshark, and it looks like a normal get request, no connect commands in sight:

GET http://proxyjudge.iathao.com/ HTTP/1.1
Host: proxyjudge.iathao.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Proxy-Connection: keep-alive

======================================================

HTTP/1.0 200 OK
Date: Mon, 27 Aug 2012 13:34:39 GMT
Server: Apache
X-Powered-By: PHP/5.3.15
Content-Type: text/html
X-Cache: MISS from ****
X-Cache-Lookup: MISS from ****:****
Connection: close

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>AZ Environment variables 1.04</title>
</head>
<body>
<pre>
HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:13.0) Gecko/20100101 Firefox/13.0.1
HTTP_HOST = proxyjudge.iathao.com
REQUEST_URI = /
HTTP_CONNECTION = keep-alive
REMOTE_PORT = 46153
HTTP_ACCEPT_LANGUAGE = en-us,en;q=0.5
HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
REMOTE_ADDR = ****
HTTP_CACHE_CONTROL = max-age=259200
HTTP_ACCEPT_ENCODING = gzip, deflate
REQUEST_METHOD = GET
REQUEST_TIME = 1346074479
</pre>
</body>
</html>

Everywhere I read that CONNECT is necessary, yet here it causes an error. And if connect is not necessary, how do I authorize the connection at password protected proxys?

Can anyone recommend a good article on this topic?

As far as I know you should only issue the CONNECT command to the proxy, and get the response. Not the HOST command. If OK, everything else should be legal HTTP intended for the upstream server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM