Loading

C# Post

  1 /// <param name="para_postDataByte">字节集提交, 欲提交的字节集数据</param>
  2             /// <param name="para_method">访问方式, 0=GET 1=POST 2=HEAD 3=PUT  4=OPTIONS  5=DELETE  6=TRACE  7=CONNECT</param>
  3             /// <param name="para_postData">字符串信息提交, "POST"专用,方法内部自动将字符串进行UTF8编码</param>
  4             /// <param name="para_requestCookies">提交Cookies, 设置提交时的cookie</param>
  5             /// <param name="para_responseCookies">返回Cookies, 返回的Cookie</param>
  6             /// <param name="para_requestHeader">附加协议头, 一行一个请用换行符隔开</param>
  7             /// <param name="para_responseHeader">返回协议头, 访问网页时返回的协议头</param>
  8             /// <param name="para_responseStatusCode">返回状态代码, 访问网页时返回的状态代码,例如:200;302;404等</param>
  9             /// <param name="para_forbidDirect">禁止重定向, 默认不禁止网页重定向</param>
 10             /// <param name="para_proxyAdd">代{过}{滤}理地址, 格式为 8.8.8.8:88</param>
 11             /// <param name="para_timeout">网页访问超时, 单位:毫秒,默认为15000毫秒, -1为无限等待</param>
 12             /// <param name="para_proxyUser">代{过}{滤}理用户名</param>
 13             /// <param name="para_proxyPassw">代{过}{滤}理密码</param>
 14             /// <param name="para_proxyMark">代{过}{滤}理标识,默认为1,0为路由器</param>
 15             /// <param name="para_isUpdateCookies">是否自动合并更新Cookie, 默认为真,自动合并更新</param>
 16             /// <param name="isSupplyHeader">是否补全必要协议头,当附件协议头为空时自动添加必要的UA协议头 默认为真,假将不再添加非传入协议头</param>
 17             /// <param name="isResolveHeaderUandL">是否处理协议头大小写,将协议头中的键名首字母处理为大写  默认为真</param>
 18 public byte[] WebBrowse_winhttp(string para_url, byte[] para_postDataByte, int para_method = 0, string para_postData = "", string para_requestCookies = "", string para_responseCookies = "", string para_requestHeader = "", string para_responseHeader = "", int para_responseStatusCode = 200, bool para_forbidDirect = false, string para_proxyAdd = "", int para_timeout = 15000, string para_proxyUser = "", string para_proxyPassw = "", int para_proxyMark = 1, bool para_isUpdateCookies = true, bool isSupplyHeader = true, bool isResolveHeaderUandL = true)
 19             {
 20                  
 21                 if (para_method < 0 || para_method > 7) { para_method = 0; }
 22                 string[] browseMethodArr = new string[] { "GET", "POST", "HEAD", "PUT", "OPTIONS", "DELETE", "TRACE", "CONNECT" };
 23                 string browseMethod = browseMethodArr[para_method];
 24                 WinHttpRequest winhttpReq = new WinHttpRequest();//创建WinHttpRequest
 25                  
 26                 if (para_timeout != -1)//超时设置
 27                 {
 28                     if (para_timeout < 1) { para_timeout = 15000; }
 29                 }
 30                 winhttpReq.SetTimeouts(para_timeout, para_timeout, para_timeout, para_timeout);
 31  
 32                 if (para_proxyAdd != "")//代{过}{滤}理设置
 33                 {
 34                     winhttpReq.SetProxy(2, para_proxyAdd);
 35                 }
 36                 winhttpReq.Open(browseMethodArr[para_method], para_url, false);//winhttpReq.Open("请求方式","请求地址",是否异步请求)
 37  
 38                 if (para_forbidDirect)//是否禁止重定向
 39                 {
 40                     winhttpReq.Option[WinHttpRequestOption.WinHttpRequestOption_EnableRedirects] = 0;
 41                 }
 42                 winhttpReq.Option[WinHttpRequestOption.WinHttpRequestOption_SslErrorIgnoreFlags] = 13056;//忽略错误标志
 43  
 44                 if (isSupplyHeader)//是否补全必要协议头,当附件协议头为空时自动添加必要的UA协议头 默认为真
 45                 {
 46                     if (para_requestHeader.IndexOf("Accept:") == -1) { para_requestHeader += "\r\nAccept: */*"; }
 47                     if (para_requestHeader.IndexOf("Referer:") == -1) { para_requestHeader += "\r\nReferer: " + para_url; }
 48                     if (para_requestHeader.IndexOf("Accept-Language:") == -1) { para_requestHeader += "\r\nAccept-Language: zh-cn"; }
 49                     if (para_requestHeader.IndexOf("User-Agent:") == -1) { para_requestHeader += "\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"; }
 50                     if (para_method == 1)
 51                     {
 52                         if (para_requestHeader.IndexOf("Content-Type:") == -1) { para_requestHeader += "\r\nContent-Type: application/x-www-form-urlencoded"; }
 53                     }
 54                 }
 55  
 56                 if (para_proxyAdd != "" && para_proxyUser != "")
 57                 {
 58                     winhttpReq.SetCredentials(para_proxyUser, para_proxyPassw, para_proxyMark);
 59                     if (para_proxyMark == 1) { winhttpReq.SetRequestHeader("Proxy-Authorization", "Basic " + TextOperate.EncodeAndDecode.EncodeBase64("gbk", para_proxyUser + ":" + para_proxyPassw)); }
 60                 }
 61  
 62                 if (para_requestCookies != null && para_requestCookies.Length > 0)
 63                 {
 64                     if (para_requestCookies.IndexOf("Cookie:", StringComparison.OrdinalIgnoreCase) != -1) {
 65                         para_requestCookies = TextOperate.TextUniversallyOperate.StrReplace(para_requestCookies, "Cookie:","");
 66                     }
 67                     winhttpReq.SetRequestHeader("Cookie", para_requestCookies);
 68                 }
 69                  
 70                 if (para_requestHeader.IndexOf("\n") == -1)
 71                 {
 72                     Dictionary<string, string> headerInfo = GetNameAndValueFromeRequestHeader(para_requestHeader);
 73                     winhttpReq.SetRequestHeader(headerInfo["headerName"], headerInfo["headerValue"]);
 74                 }
 75                 else
 76                 {
 77                     string[] headerArr = para_requestHeader.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
 78                     foreach (string headerArrItem in headerArr)
 79                     {
 80                         Dictionary<string, string> headerInfo = GetNameAndValueFromeRequestHeader(headerArrItem);
 81                         if (headerInfo["headerName"] == "Connection" && headerInfo["headerValue"] == "keep-alive") { continue; }//筛选排除此参数 避免协议头有此参数 HTTP对象又自动添加此参数
 82                         winhttpReq.SetRequestHeader(headerInfo["headerName"], headerInfo["headerValue"]);
 83                     }
 84                 }
 85  
 86                 if (para_postDataByte == null || para_postDataByte.Length == 0)
 87                 {
 88                     winhttpReq.Send(para_postData);//发送
 89                 }
 90                 else
 91                 {
 92                     winhttpReq.Send(para_postDataByte);//发送
 93                 }
 94                 byte[] responseBody_b = winhttpReq.ResponseBody;//返回的数据 用byte数组接收   注:winhttpReq.ResponseText直接输出有中文乱码的情况
 95                 //string docHtml = System.Text.Encoding.UTF8.GetString(responseBody_b);//转码
 96                 para_responseHeader = winhttpReq.GetAllResponseHeaders();
 97                 para_responseStatusCode = winhttpReq.Status;
 98  
 99                 string[] responseHeaderArr = para_responseHeader.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
100                 para_responseCookies = "";
101                 foreach (string responseHeaderArrItem in responseHeaderArr)//取返回的cookie
102                 {
103                     if (TextOperate.TextUniversallyOperate.GetLeftStrByTargetStr(responseHeaderArrItem, ": ") == "Set-Cookie")
104                     {
105                         if (responseHeaderArrItem.IndexOf(";") != -1)
106                         {
107                             para_responseCookies += TextOperate.TextUniversallyOperate.GetMidStr(responseHeaderArrItem, "Set-Cookie:", ";").Trim() + ";";
108                         }
109                         else
110                         {
111                             para_responseCookies += TextOperate.TextUniversallyOperate.StrReplace(responseHeaderArrItem, "Set-Cookie:", "").Trim() + ";";
112                         }
113                     }
114                 }
115                 if (para_responseCookies.Length >= 2 && para_responseCookies.Substring(para_responseCookies.Length - 2) == "; ")
116                 {
117                     para_responseCookies = para_responseCookies.Substring(0, para_responseCookies.Length - 2);
118                 }
119                 if (para_isUpdateCookies)
120                 {
121                     if (para_requestCookies != null && para_requestCookies.Length != 0) { para_responseCookies = UpdateCookies(para_requestCookies, para_responseCookies); }
122                 }
123                 //Console.WriteLine(winhttpReq.GetAllResponseHeaders());
124                 return responseBody_b;
125             }

 

posted @ 2020-07-02 16:34  jevan  阅读(279)  评论(0编辑  收藏  举报