HttpWebRequest的通过两个代理 [英] HttpWebRequest through two proxies

查看:127
本文介绍了HttpWebRequest的通过两个代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近建立了一个网站,它使用的地理DNS根据您的位置,以解决DNS两个不同的IP的。

I have recently set up a website which uses geographic DNS to resolve the DNS to two different IP's depending on your location.

不过,这种手段来监测网站,我需要确保该网站是在这两个地理位置可用。要做到这一点,我在.net中写了一个小程序来不断地尝试和HttpWebRequest的获得,一旦使用本地网络设置,并一度使用基于该地区的代理网站上的一个小的HTML文件,将名称解析为第二个IP地址

However, this means to monitor the websites, I need to make sure the site is available in both geographical locations. To do this I wrote a small program in .net to continually try and HttpWebRequest to get a small html file on the website once using the local internet settings and once using a proxy based in the region which will resolve the name to the second IP address.

这工作正常在我的笔记本电脑在家里,但在办公室,连接到互联网上几乎所有的机器,你需要通过一个代理,这意味着去我把前面不再代理工作。

This works fine on my laptop at home, however in the office, to connect to the internet on almost all machines you will need to go via a proxy, which means the proxy I set earlier no longer works.

我需要什么,能够做的就是通过办公室代理在僻远的乡村发送请求,然后通过代理和终于到了网站上。

What I need to be able to do is send the request through the office proxy, then through the proxy in the remote country and finally to the website.

让我知道,如果这还不够清楚!

Let me know if this is not clear enough!

推荐答案

首先,你需要确保两个代理是HTTPS和他们都支持的连接方法,即代理链。通常的HTTP协议的设计不提供代理链的支持。
中的想法是要建立2 CONNECT隧道,1个内另一个。
算法如下:

First you need to ensure that both proxies are HTTPS and they both support CONNECT method, i.e. "proxy chaining". Design of usual HTTP protocol doesn't provide support for "proxy chaining". The idea is to establish 2 CONNECT tunnels, one inside another. The algorithm is the following:


  1. 通过TCP连接到第一个代理

  2. 请求CONNECT隧道第二代理

  3. 一旦创建隧道,隧道的要求向目标主机

  4. 发送请求。请求将前往目标通过代理#1和#代理主机2。
    下面是一个示例代码,我在我的箱子进行了测试:

  1. Connect to the 1st proxy via TCP
  2. Request CONNECT tunnel to 2nd proxy
  3. Once tunnel is created, request tunnel to target host
  4. Send request. Request will go to target host through proxy #1 and proxy #2. Below is a sample code which I have tested on my box:

string host = "encrypted.google.com";
string proxy2 = "213.240.237.149";//host;
int proxyPort2 = 3128;//443;
string proxy = "180.183.236.63";//host;
int proxyPort = 3128;//443;

byte[] buffer = new byte[2048];
int bytes;

// Connect to the 1st proxy
TcpClient client = new TcpClient(proxy, proxyPort);
NetworkStream stream = client.GetStream();

// Establish tunnel to 2nd proxy
byte[] tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:{1}  HTTP/1.1\r\nHost:{0}\r\n\r\n", proxy2, proxyPort2));
stream.Write(tunnelRequest, 0, tunnelRequest.Length);
stream.Flush();

// Read response to CONNECT request
// There should be loop that reads multiple packets
bytes = stream.Read(buffer, 0, buffer.Length);
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));

// Establish tunnel to target host
tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:443  HTTP/1.1\r\nHost:{0}\r\n\r\n", host));
stream.Write(tunnelRequest, 0, tunnelRequest.Length);
stream.Flush();

// Read response to CONNECT request
// There should be loop that reads multiple packets
bytes = stream.Read(buffer, 0, buffer.Length);
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));

// Wrap into SSL stream
SslStream sslStream2 = new SslStream(stream);
sslStream2.AuthenticateAsClient(host);

// Send request
byte[] request = Encoding.UTF8.GetBytes(String.Format("GET https://{0}/  HTTP/1.1\r\nHost: {0}\r\n\r\n", host));
sslStream2.Write(request, 0, request.Length);
sslStream2.Flush();

// Read response
do
{
    bytes = sslStream2.Read(buffer, 0, buffer.Length);
    Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
} while (bytes != 0);

client.Close();
Console.ReadKey();


这篇关于HttpWebRequest的通过两个代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆