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

本文关键字:连接 网站 代理服务器 免费 编程 方式使 | 更新日期: 2023-09-27 18:35:30

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

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

谢谢

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

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

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);
您可以使用

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;