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
    大概是这样、
  • 相关阅读:
    Django关于StreamingHttpResponse与FileResponse响应文件或视频的下载请求
    APScheduler可能遇到的问题
    django中model聚合使用
    Java 递归判断迷宫问题是否有路
    direct path read/write (直接路径读/写)
    DRM 简介
    SQL Server2008表名中含“.”号处理方法
    Java学习之:JDK动态代理与CGLIB动态代理
    强大易用!新一代爬虫利器 Playwright
    为什么cudaMalloc()参数是二级指针
  • 原文地址:https://www.cnblogs.com/hzpin/p/6243973.html
Copyright © 2011-2022 走看看