HttpHelper Http/Https请求方式

前言:最近在对接接口时发现其他项目写的Http/Https请求很多都不能同用(都是按照当时接口进行编写的),现在整理一份HttpHelper来实现大多数场景。哈哈.... 不讲废话了,代码贴出来了

 

HttpHelper Http/Https请求方式

1.HttpHelper.cs

 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.IO.Compression; 5 using System.Linq; 6 using System.Net; 7 using System.Net.Security; 8 using System.Security.Cryptography.X509Certificates; 9 using System.Text; 10 using System.Text.RegularExpressions; 11 using System.Threading.Tasks; 12  13 namespace Own.Common 14 { 15 /// <summary> 16 /// Http连接操作帮助类 17 /// </summary> 18 public class HttpHelper 19  { 20 #region 预定义方法或者变更 21 //默认的编码 22 private Encoding encoding = Encoding.Default; 23 //Post数据编码 24 private Encoding postencoding = Encoding.Default; 25 //HttpWebRequest对象用来发起请求 26 private HttpWebRequest request = null; 27 //获取影响流的数据对象 28 private HttpWebResponse response = null; 29 /// <summary> 30 /// 根据相传入的数据,得到相应页面数据 31 /// </summary> 32 /// <param name="item">参数类对象</param> 33 /// <returns>返回HttpResult类型</returns> 34 public HttpResult GetHtml(HttpItem item) 35  { 36 //返回参数 37 HttpResult result = new HttpResult(); 38 try 39  { 40 //准备参数 41  SetRequest(item); 42  } 43 catch (Exception ex) 44  { 45 return new HttpResult() { Cookie = string.Empty, Header = null, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message }; 46  } 47 try 48  { 49 #region 得到请求的response 50 if (item.ResultCookieType == ResultCookieType.CookieCollection) 51 request.CookieContainer = new CookieContainer(); 52 using (response = (HttpWebResponse)request.GetResponse()) 53  { 54 result.StatusCode = response.StatusCode; 55 result.StatusDescription = response.StatusDescription; 56 result.Header = response.Headers; 57 if (response.Cookies != null) result.CookieCollection = response.Cookies; 58 if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"]; 59 byte[] ResponseByte = null; 60 using (MemoryStream _stream = new MemoryStream()) 61  { 62 //GZIIP处理 63 if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) 64  { 65 //开始读取流并设置编码方式 66 new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream); 67  } 68 else 69  { 70 //开始读取流并设置编码方式 71  response.GetResponseStream().CopyTo(_stream); 72  } 73 //获取Byte 74 ResponseByte = _stream.ToArray(); 75  } 76 if (ResponseByte != null & ResponseByte.Length > 0) 77  { 78 //是否返回Byte类型数据 79 if (item.ResultType == ResultType.Byte) result.ResultByte = ResponseByte; 80 //从这里开始我们要无视编码了 81 if (encoding == null) 82  { 83 Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta([^<]*)charset=([^<]*)[\"‘]", RegexOptions.IgnoreCase); 84 string c = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower().Trim() : string.Empty; 85 if (c.Length > 2) 86  { 87 try 88  { 89 if (c.IndexOf(" ") > 0) c = c.Substring(0, c.IndexOf(" ")); 90 encoding = Encoding.GetEncoding(c.Replace("\"", "").Replace("", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim()); 91  } 92 catch 93  { 94 if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8; 95 else encoding = Encoding.GetEncoding(response.CharacterSet); 96  } 97  } 98 else 99  {100 if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;101 else encoding = Encoding.GetEncoding(response.CharacterSet);102  }103  }104 //得到返回的HTML105 result.Html = encoding.GetString(ResponseByte);106  }107 else108  {109 //得到返回的HTML110 result.Html = "本次请求并未返回任何数据";111  }112  }113 #endregion114  }115 catch (WebException ex)116  {117 //这里是在发生异常时返回的错误信息118 response = (HttpWebResponse)ex.Response;119 result.Html = ex.Message;120 if (response != null)121  {122 result.StatusCode = response.StatusCode;123 result.StatusDescription = response.StatusDescription;124  }125  }126 catch (Exception ex)127  {128 result.Html = ex.Message;129  }130 if (item.IsToLower) result.Html = result.Html.ToLower();131 return result;132  }133 /// <summary>134 /// 为请求准备参数135 /// </summary>136 ///<param name="item">参数列表</param>137 private void SetRequest(HttpItem item)138  {139 // 验证证书140  SetCer(item);141 //设置Header参数142 if (item.Header != null && item.Header.Count > 0) foreach (string key in item.Header.AllKeys)143  {144  request.Headers.Add(key, item.Header[key]);145  }146 // 设置代理147  SetProxy(item);148 if (item.ProtocolVersion != null) request.ProtocolVersion = item.ProtocolVersion;149 request.ServicePoint.Expect100Continue = item.Expect100Continue;150 //请求方式Get或者Post151 request.Method = item.Method;152 request.Timeout = item.Timeout;153 request.KeepAlive = item.KeepAlive;154 request.ReadWriteTimeout = item.ReadWriteTimeout;155 if (!string.IsNullOrWhiteSpace(item.Host))156  {157 request.Host = item.Host;158  }159 //Accept160 request.Accept = item.Accept;161 //ContentType返回类型162 request.ContentType = item.ContentType;163 //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息164 request.UserAgent = item.UserAgent;165 // 编码166 encoding = item.Encoding;167 //设置Cookie168  SetCookie(item);169 //来源地址170 request.Referer = item.Referer;171 //是否执行跳转功能172 request.AllowAutoRedirect = item.Allowautoredirect;173 //设置Post数据174  SetPostData(item);175 //设置最大连接176 if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit;177  }178 /// <summary>179 /// 设置证书180 /// </summary>181 /// <param name="item"></param>182 private void SetCer(HttpItem item)183  {184 185 if (!string.IsNullOrWhiteSpace(item.CerPath))186  {187 //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。188 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);189 //初始化对像,并设置请求的URL地址190 request = (HttpWebRequest)WebRequest.Create(item.URL);191  SetCerList(item);192 //将证书添加到请求里193 request.ClientCertificates.Add(new X509Certificate(item.CerPath));194  }195 else if (string.IsNullOrWhiteSpace(item.CerPath) && item.URL.IsContains("https"))196  {197 //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。198 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);199 //初始化对像,并设置请求的URL地址200 request = (HttpWebRequest)WebRequest.Create(item.URL);201  SetCerList(item);202  }203 else204  {205 //初始化对像,并设置请求的URL地址206 request = (HttpWebRequest)WebRequest.Create(item.URL);207  SetCerList(item);208  }209  }210 /// <summary>211 /// 设置多个证书212 /// </summary>213 /// <param name="item"></param>214 private void SetCerList(HttpItem item)215  {216 if (item.ClentCertificates != null && item.ClentCertificates.Count > 0)217  {218 foreach (X509Certificate c in item.ClentCertificates)219  {220  request.ClientCertificates.Add(c);221  }222  }223  }224 /// <summary>225 /// 设置Cookie226 /// </summary>227 /// <param name="item">Http参数</param>228 private void SetCookie(HttpItem item)229  {230 if (!string.IsNullOrWhiteSpace(item.Cookie))231 //Cookie232 request.Headers[HttpRequestHeader.Cookie] = item.Cookie;233 //设置Cookie234 if (item.CookieCollection != null && item.CookieCollection.Count > 0)235  {236 request.CookieContainer = new CookieContainer();237  request.CookieContainer.Add(item.CookieCollection);238  }239  }240 /// <summary>241 /// 设置Post数据242 /// </summary>243 /// <param name="item">Http参数</param>244 private void SetPostData(HttpItem item)245  {246 //验证在得到结果时是否有传入数据247 if (request.Method.Trim().ToLower().Contains("post"))248  {249 if (item.PostEncoding != null)250  {251 postencoding = item.PostEncoding;252  }253 byte[] buffer = null;254 //写入Byte类型255 if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0)256  {257 //验证在得到结果时是否有传入数据258 buffer = item.PostdataByte;259 }//写入文件260 else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrWhiteSpace(item.Postdata))261  {262 StreamReader r = new StreamReader(item.Postdata, postencoding);263 buffer = postencoding.GetBytes(r.ReadToEnd());264  r.Close();265 } //写入字符串266 else if (!string.IsNullOrWhiteSpace(item.Postdata) || item.PostdataDic.Count>0)267  {268 if(!string.IsNullOrWhiteSpace(item.Postdata))269 buffer = postencoding.GetBytes(item.Postdata);270 else271  {272 var strBuffer = new StringBuilder();273 var i = 0;274 foreach (string key in item.PostdataDic.Keys)275  {276 if (i > 0)277  {278 strBuffer.AppendFormat("&{0}={1}", key, item.PostdataDic[key]);279  }280 else281  {282 strBuffer.AppendFormat("{0}={1}", key, item.PostdataDic[key]);283  }284 i++;285  }286 287 buffer = postencoding.GetBytes(strBuffer.ToString());288  }289  }290 if (buffer != null)291  {292 request.ContentLength = buffer.Length;293 using (var stream = request.GetRequestStream())294  {295 stream.Write(buffer, 0, buffer.Length);296  }297  }298  }299  }300 /// <summary>301 /// 设置代理302 /// </summary>303 /// <param name="item">参数对象</param>304 private void SetProxy(HttpItem item)305  {306 if (!string.IsNullOrWhiteSpace(item.ProxyIp))307  {308 //设置代理服务器309 if (item.ProxyIp.Contains(":"))310  {311 string[] plist = item.ProxyIp.Split(:);312 WebProxy myProxy = new WebProxy(plist[0].Trim(), Convert.ToInt32(plist[1].Trim()));313 //建议连接314 myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);315 //给当前请求对象316 request.Proxy = myProxy;317  }318 else319  {320 WebProxy myProxy = new WebProxy(item.ProxyIp, false);321 //建议连接322 myProxy.Credentials = new NetworkCredential(item.ProxyUserName, item.ProxyPwd);323 //给当前请求对象324 request.Proxy = myProxy;325  }326 //设置安全凭证327 request.Credentials = CredentialCache.DefaultNetworkCredentials;328  }329  }330 /// <summary>331 /// 回调验证证书问题332 /// </summary>333 /// <param name="sender">流对象</param>334 /// <param name="certificate">证书</param>335 /// <param name="chain">X509Chain</param>336 /// <param name="errors">SslPolicyErrors</param>337 /// <returns>bool</returns>338 public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; }339 340 #endregion341  }342 /// <summary>343 /// Http请求参考类344 /// </summary>345 public class HttpItem346  {347 /// <summary>348 /// 请求URL必须填写349 /// </summary>350 public string URL { get; set; }351 string _Method = "GET";352 /// <summary>353 /// 请求方式默认为GET方式,当为POST方式时必须设置Postdata的值354 /// </summary>355 public string Method356  {357 get { return _Method; }358 set { _Method = value; }359  }360 int _Timeout = 100000;361 /// <summary>362 /// 默认请求超时时间363 /// </summary>364 public int Timeout365  {366 get { return _Timeout; }367 set { _Timeout = value; }368  }369 int _ReadWriteTimeout = 30000;370 /// <summary>371 /// 默认写入Post数据超时间372 /// </summary>373 public int ReadWriteTimeout374  {375 get { return _ReadWriteTimeout; }376 set { _ReadWriteTimeout = value; }377  }378 /// <summary>379 /// 设置Host的标头信息380 /// </summary>381 public string Host { get; set; }382 Boolean _KeepAlive = true;383 /// <summary>384 /// 获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接默认为true。385 /// </summary>386 public Boolean KeepAlive387  {388 get { return _KeepAlive; }389 set { _KeepAlive = value; }390  }391 string _Accept = "text/html, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";392 /// <summary>393 /// 请求标头值 默认为text/html, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*394 /// </summary>395 public string Accept396  {397 get { return _Accept; }398 set { _Accept = value; }399  }400 string _ContentType = "application/x-www-form-urlencoded";401 /// <summary>402 /// 请求返回类型默认 403 /// </summary>404 public string ContentType405  {406 get { return _ContentType; }407 set { _ContentType = value; }408  }409 string _UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";410 /// <summary>411 /// 客户端访问信息默认Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)412 /// </summary>413 public string UserAgent414  {415 get { return _UserAgent; }416 set { _UserAgent = value; }417  }418 /// <summary>419 /// 返回数据编码默认为NUll,可以自动识别,一般为utf-8,gbk,gb2312420 /// </summary>421 public Encoding Encoding { get; set; }422 private PostDataType _PostDataType = PostDataType.String;423 /// <summary>424 /// Post的数据类型425 /// </summary>426 public PostDataType PostDataType427  {428 get { return _PostDataType; }429 set { _PostDataType = value; }430  }431 432 /// <summary>433 /// Post请求时要发送的字符串Post数据434 /// </summary>435 public string Postdata { get; set; }436 437 /// <summary>438 /// Post请求时要发送的Post数据439 /// </summary>440 public IDictionary<string, string> PostdataDic { get; set; }441 442 /// <summary>443 /// Post请求时要发送的Byte类型的Post数据444 /// </summary>445 public byte[] PostdataByte { get; set; }446 /// <summary>447 /// Cookie对象集合448 /// </summary>449 public CookieCollection CookieCollection { get; set; }450 /// <summary>451 /// 请求时的Cookie452 /// </summary>453 public string Cookie { get; set; }454 /// <summary>455 /// 来源地址,上次访问地址456 /// </summary>457 public string Referer { get; set; }458 /// <summary>459 /// 证书绝对路径460 /// </summary>461 public string CerPath { get; set; }462 private Boolean isToLower = false;463 /// <summary>464 /// 是否设置为全文小写,默认为不转化465 /// </summary>466 public Boolean IsToLower467  {468 get { return isToLower; }469 set { isToLower = value; }470  }471 private Boolean allowautoredirect = false;472 /// <summary>473 /// 支持跳转页面,查询结果将是跳转后的页面,默认是不跳转474 /// </summary>475 public Boolean Allowautoredirect476  {477 get { return allowautoredirect; }478 set { allowautoredirect = value; }479  }480 private int connectionlimit = 1024;481 /// <summary>482 /// 最大连接数483 /// </summary>484 public int Connectionlimit485  {486 get { return connectionlimit; }487 set { connectionlimit = value; }488  }489 /// <summary>490 /// 代理Proxy 服务器用户名491 /// </summary>492 public string ProxyUserName { get; set; }493 /// <summary>494 /// 代理 服务器密码495 /// </summary>496 public string ProxyPwd { get; set; }497 /// <summary>498 /// 代理 服务IP499 /// </summary>500 public string ProxyIp { get; set; }501 private ResultType resulttype = ResultType.String;502 /// <summary>503 /// 设置返回类型String和Byte504 /// </summary>505 public ResultType ResultType506  {507 get { return resulttype; }508 set { resulttype = value; }509  }510 private WebHeaderCollection header = new WebHeaderCollection();511 /// <summary>512 /// header对象513 /// </summary>514 public WebHeaderCollection Header515  {516 get { return header; }517 set { header = value; }518  }519 /// <summary>520 // 获取或设置用于请求的 HTTP 版本。返回结果:用于请求的 HTTP 版本。默认为 System.Net.HttpVersion.Version11。521 /// </summary>522 public Version ProtocolVersion { get; set; }523 private Boolean _expect100continue = true;524 /// <summary>525 /// 获取或设置一个 System.Boolean 值,该值确定是否使用 100-Continue 行为。如果 POST 请求需要 100-Continue 响应,则为 true;否则为 false。默认值为 true。526 /// </summary>527 public Boolean Expect100Continue528  {529 get { return _expect100continue; }530 set { _expect100continue = value; }531  }532 /// <summary>533 /// 设置509证书集合534 /// </summary>535 public X509CertificateCollection ClentCertificates { get; set; }536 /// <summary>537 /// 设置或获取Post参数编码,默认的为Default编码538 /// </summary>539 public Encoding PostEncoding { get; set; }540 private ResultCookieType _ResultCookieType = ResultCookieType.String;541 /// <summary>542 /// Cookie返回类型,默认的是只返回字符串类型543 /// </summary>544 public ResultCookieType ResultCookieType545  {546 get { return _ResultCookieType; }547 set { _ResultCookieType = value; }548  }549  }550 /// <summary>551 /// Http返回参数类552 /// </summary>553 public class HttpResult554  {555 /// <summary>556 /// Http请求返回的Cookie557 /// </summary>558 public string Cookie { get; set; }559 /// <summary>560 /// Cookie对象集合561 /// </summary>562 public CookieCollection CookieCollection { get; set; }563 /// <summary>564 /// 返回的String类型数据 只有ResultType.String时才返回数据,其它情况为空565 /// </summary>566 public string Html { get; set; }567 /// <summary>568 /// 返回的Byte数组 只有ResultType.Byte时才返回数据,其它情况为空569 /// </summary>570 public byte[] ResultByte { get; set; }571 /// <summary>572 /// header对象573 /// </summary>574 public WebHeaderCollection Header { get; set; }575 /// <summary>576 /// 返回状态说明577 /// </summary>578 public string StatusDescription { get; set; }579 /// <summary>580 /// 返回状态码,默认为OK581 /// </summary>582 public HttpStatusCode StatusCode { get; set; }583  }584 /// <summary>585 /// 返回类型586 /// </summary>587 public enum ResultType588  {589 /// <summary>590 /// 表示只返回字符串 只有Html有数据591 /// </summary>592  String,593 /// <summary>594 /// 表示返回字符串和字节流 ResultByte和Html都有数据返回595 /// </summary>596  Byte597  }598 /// <summary>599 /// Post的数据格式默认为string600 /// </summary>601 public enum PostDataType602  {603 /// <summary>604 /// 字符串类型,这时编码Encoding可不设置605 /// </summary>606  String,607 /// <summary>608 /// Byte类型,需要设置PostdataByte参数的值编码Encoding可设置为空609 /// </summary>610  Byte,611 /// <summary>612 /// 传文件,Postdata必须设置为文件的绝对路径,必须设置Encoding的值613 /// </summary>614  FilePath615  }616 /// <summary>617 /// Cookie返回类型618 /// </summary>619 public enum ResultCookieType620  {621 /// <summary>622 /// 只返回字符串类型的Cookie623 /// </summary>624  String,625 /// <summary>626 /// CookieCollection格式的Cookie集合同时也返回String类型的cookie627 /// </summary>628  CookieCollection629  }630 631 }

 

2.简洁模拟HTTP请求

 1 /// <summary> 2 /// 简洁模拟HTTP请求 3 /// </summary> 4 /// <param name="requestUrl">请求的Url地址</param> 5 /// <param name="requestMethod">目前支持字符串格式的get或post</param> 6 /// <param name="postData">post请求时提供如下格式数据:a=1&b=2&c=3...</param> 7 /// <param name="requestCookie">如果需要cookie请带上,格式:a=1;b=2;c=3...</param> 8 /// <param name="requestReferer">来源页面地址</param> 9 /// <returns></returns>10 public HttpResult GetHtml(string requestUrl, string requestMethod = "get", string postData = "", string requestCookie = "", string requestReferer = "")11  {12 string requestContentType = requestMethod == "get" ? "text/html" : "application/x-www-form-urlencoded";13 HttpItem item = new HttpItem()14  {15 URL = requestUrl,//URL 必需项 16 Method = requestMethod,//URL 可选项 默认为Get 17 IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写 18 Cookie = requestCookie,//字符串Cookie 可选项 19 Referer = requestReferer,//来源URL 可选项 20 Postdata = postData,//Post数据 可选项GET时不需要写 21 Timeout = 100000,//连接超时时间 可选项默认为100000 22 ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000 23 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",24 ContentType = requestContentType,//返回类型 可选项有默认值 25 Allowautoredirect = false,//是否根据301跳转 可选项 26 ResultType = ResultType.String,27 Encoding = Encoding.UTF8,28 PostEncoding = Encoding.UTF829  };30 return GetHtml(item);31  }32 public byte[] GetImage(string sourceImgUrl)33  {34 HttpItem item = new HttpItem()35  {36 URL = sourceImgUrl,//URL 必需项 37 Method = "get",//URL 可选项 默认为Get 38 IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写 39 Cookie = "",//字符串Cookie 可选项 40 Referer = "",//来源URL 可选项 41 Postdata = "",//Post数据 可选项GET时不需要写 42 Timeout = 100000,//连接超时时间 可选项默认为100000 43 ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000 44 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 45 ResultType = ResultType.Byte46  };47 HttpResult result = GetHtml(item);48 return result.ResultByte;49  }50 public byte[] GetAudio(string sourceUrl, string cookie)51  {52 HttpItem item = new HttpItem()53  {54 URL = sourceUrl,//URL 必需项 55 Method = "get",//URL 可选项 默认为Get 56 IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写 57 Cookie = cookie,//字符串Cookie 可选项 58 Referer = "",//来源URL 可选项 59 Postdata = "",//Post数据 可选项GET时不需要写 60 Timeout = 100000,//连接超时时间 可选项默认为100000 61 ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000 62 UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 63 ResultType = ResultType.Byte,64 ContentType = "audio/mpeg"65  };66 HttpResult result = GetHtml(item);67 return result.ResultByte;68 }

 

相关文章