C# 通过代理获取url数据


 1 public static string doPost(string Url, byte[] postData, SinaCookie bCookie, String encodingFormat, String referer, string ProxyStr)
 2         {
 3                 try
 4                 {
 5                     HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url.ToString());
 6                     if (ProxyStr != ""&&ProxyStr != null)
 7                     {
 8                         //设置代理
 9                         WebProxy proxy = new WebProxy();
10                         proxy.Address = new Uri(ProxyStr);
11                         myRequest.UseDefaultCredentials = true;
12                         myRequest.Proxy = proxy;
13                     }
14                     //myRequest.ServicePoint.Expect100Continue = false;
15                     myRequest.CookieContainer = bCookie.mycookie;
16                     myRequest.Method = "POST";
17                     myRequest.Timeout = 30000;
18                     myRequest.KeepAlive = true;//modify by yang
19                     if (referer != "")
20                         myRequest.Referer = referer;
21                     myRequest.Headers["Cache-control"] = "no-cache";//.CachePolicy = .c "no-cache";//["Cache-control: no-cache"]
22                     myRequest.Headers["Accept-Language"] = "zh-cn";
23                     //myRequest.Headers["x-requested-with"] = "XMLHttpRequest";
24                     myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Trident/4.0; GTB7.4; GTB7.1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)";
25                     myRequest.ContentType = "application/x-www-form-urlencoded";
26                     myRequest.Accept = "*/*";
27                     myRequest.ContentLength = postData.Length;
28  
29                     //setRequestHeader(requestHearder, myRequest);
30  
31                     Stream newStream = myRequest.GetRequestStream();
32                     newStream.Write(postData, 0, postData.Length);
33                     newStream.Close();
34                     //if (waitTime != 0)
35                     //    Thread.Sleep(waitTime);
36                     HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
37                     bCookie.upcookie(myResponse.Cookies);
38                     StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding(encodingFormat));
39                     string outdata = reader.ReadToEnd();
40                     reader.Close();
41                     if (!outdata.Contains("基础连接已经关闭: 连接被意外关闭") && !outdata.Contains("无法连接到远程服务器") && !outdata.Contains("基础连接已经关闭: 接收时发生错误。"))
42                         return outdata;
43                     else
44                         return "基础连接已经关闭: 连接被意外关闭";
45  
46                 }
47                 catch (Exception ex)
48                 {
49                     if (!ex.Message.Contains("基础连接已经关闭: 连接被意外关闭") && !ex.Message.Contains("无法连接到远程服务器") && !ex.Message.Contains("基础连接已经关闭: 接收时发生错误。"))
50                         return ex.Message;
51                     else
52                         return "基础连接已经关闭: 连接被意外关闭";
53                 }
54  
55         }

 

ProxyStr格式
http://192.168.1.1:80
大概是这样、

 

posted @ 2017-01-03 10:25  山间行者  阅读(549)  评论(0编辑  收藏  举报