我正在尝试使用公共代理服务器(http://www.unblockwebnow.info/)向目标网站发送HTTP请求,比如http://stackoverflow.com:)
我的HTTP客户端具有以下架构:
string url = "http://stackoverflow.com";
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWRequest.Method = "GET";
WebProxy myProxy = new WebProxy();
myProxy.Address = new Uri("http://www.unblockwebnow.info/");
HttpWRequest.Proxy = myProxy;
HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), encoding);
var rawHTML = sr.ReadToEnd();
sr.Close();
执行rawHTML的代码后,我得到"pageok -managed by puppet - hostingcms02 pageok"
如果我评论HttpWRequest.Proxy = myProxy;
行,我会收到网站内容。
答案 0 :(得分:5)
这似乎有效,但不适用于您的代理(不知道unblockwebnow.info的端口号)。 在URI
中的“:”之后添加了端口号using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string url = "http://stackoverflow.com";
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWRequest.Method = "GET";
WebProxy myProxy = new WebProxy();
//United States proxy, from http://www.hidemyass.com/proxy-list/
myProxy.Address = new Uri("http://72.64.146.136:8080");
HttpWRequest.Proxy = myProxy;
HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), true);
var rawHTML = sr.ReadToEnd();
sr.Close();
Console.Out.WriteLine(rawHTML);
Console.ReadKey();
}
}
}