6

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

任何人都可以给我一个代码片段作为示例或其他东西吗?

谢谢

4

2 回答 2

2

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

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

用于浏览谷歌。

IC#,像这样构建 url:

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);
于 2012-05-25T11:55:17.253 回答
1

您可以使用WebProxy 类

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;
于 2012-05-25T11:42:31.577 回答