C# 爬虫代理示例

阅读模式

代码示例说明

  1. 代码样例不能直接运行,请替换成您自己的代理信息。
  2. 在不同编程语言的代码示例中,需注意其环境版本。
  3. 示例代码使用遇到问题请,我们会为您提供技术支持。

request

1
2
// 要访问的目标页面
3
string targetUrl = "http://httpbin.org/ip";
4
5
6
// 代理服务器(产品官网 www.16yun.cn)
7
string proxyHost = "http://t.16yun.cn";
8
string proxyPort = "31111";
9
10
// 代理验证信息
11
string proxyUser = "username";
12
string proxyPass = "password";
13
14
// 设置代理服务器
15
WebProxy proxy = new WebProxy(string.Format("{0}:{1}", proxyHost, proxyPort), true);
16
17
18
ServicePointManager.Expect100Continue = false;
19
20
var request = WebRequest.Create(targetUrl) as HttpWebRequest;
21
22
request.AllowAutoRedirect = true;
23
request.KeepAlive = true;
24
request.Method = "GET";
25
request.Proxy = proxy;
26
27
//request.Proxy.Credentials = CredentialCache.DefaultCredentials;
28
29
request.Proxy.Credentials = new System.Net.NetworkCredential(proxyUser, proxyPass);
30
31
// 设置Proxy Tunnel
32
// Random ran=new Random();
33
// int tunnel =ran.Next(1,10000);
34
// request.Headers.Add("Proxy-Tunnel", String.valueOf(tunnel));
35
36
37
//request.Timeout = 20000;
38
//request.ServicePoint.ConnectionLimit = 512;
39
//request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36";
40
//request.Headers.Add("Cache-Control", "max-age=0");
41
//request.Headers.Add("DNT", "1");
42
43
44
//String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(proxyUser + ":" + proxyPass));
45
//request.Headers.Add("Proxy-Authorization", "Basic " + encoded);
46
47
using (var response = request.GetResponse() as HttpWebResponse)
48
using (var sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
49
{
50
string htmlStr = sr.ReadToEnd();
51
}

NOTE

如果出现服务器提交协议冲突 Section=ResponseStatusLine 异常

通过修改配置文件解决:在app.config(WinForm)或web.config(Web)文件里修改。

WinForm下的app.config默认不存在,手动在Debug文件夹所在的同级目录下新建一个XML配置文件,内容为:

1
<?xml version="1.0" encoding="utf-8" ?>
2
<configuration>
3
<system.net>
4
<settings>
5
<httpWebRequest useUnsafeHeaderParsing= "true " />
6
</settings>
7
</system.net>
8
</configuration>

编译以后会在Debug下面自动创建一个 程序名.exe.config 的配置文件