以编程方式使用免费代理服务器连接到网站

时间:2012-05-25 11:38:19

标签: c# asp.net .net proxy

我需要使用代理服务器连接到网站。我可以手动执行此操作,例如,我可以使用在线代理http://zend2.com,然后浏览到www.google.com。但这必须以编程方式完成。我知道我可以使用WebProxy类但是如何编写代码以便可以使用代理服务器?

任何人都可以给我一个代码片段作为示例或什么?

感谢

2 个答案:

答案 0 :(得分:2)

了解zend2的工作原理,您可以填充这样的网址:

  

http://zend2.com/bro.php?u=http%3A%2F%2Fwww.google.com&b=12&f=norefer

用于浏览谷歌。

我是C#,建立这样的网址:

string targetUrl = "http://www.google.com";
string proxyUrlFormat = "http://zend2.com/bro.php?u={0}&b=12&f=norefer";
string actualUrl = string.Format(proxyUrlFormat, HttpUtility.UrlEncode(targetUrl));

// Do something with the proxy-ed url
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri(actualUrl));
HttpWebResponse resp = req.GetResponse();

string content = null;
using(StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
    content = sr.ReadToEnd();
}

Console.WriteLine(content);

答案 1 :(得分:1)

您可以使用WebProxy Class

MSDN代码

WebProxy proxyObject = new WebProxy("http://proxyserver:80/",true);
WebRequest req = WebRequest.Create("http://www.contoso.com");
req.Proxy = proxyObject;

在你的情况下

WebProxy proxyObject = new WebProxy("http://zend2.com",true);
WebRequest req = WebRequest.Create("www.google.com");
req.Proxy = proxyObject;