zoukankan      html  css  js  c++  java
  • 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
    大概是这样、
  • 相关阅读:
    Java设计模式(Design Patterns)
    P2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver
    P3120 [USACO15FEB]牛跳房子(金)Cow Hopscotch (Gold)
    P4818 [USACO15DEC]Bessie's Dream 贝西的梦
    P3667 [USACO17OPEN]Bovine Genomics
    P4379 [USACO18OPEN]Lemonade Line
    P4378 [USACO18OPEN]Out of Sorts S
    P4089 [USACO17DEC]The Bovine Shuffle
    P4269 [USACO18FEB]Snow Boots G
    P4086 [USACO17DEC]My Cow Ate My Homework
  • 原文地址:https://www.cnblogs.com/hzpin/p/6243973.html
Copyright © 2011-2022 走看看