HttpWebResponseUtility.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Security;
  6. using System.Net;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace LampInspectionMachine.HttpService
  11. {
  12. /// <summary>
  13. /// 有关HTTP请求的辅助类
  14. /// </summary>
  15. public class HttpWebResponseUtility
  16. {
  17. private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
  18. /// <summary>
  19. /// 创建GET方式的HTTP请求
  20. /// </summary>
  21. /// <param name="url">请求的URL</param>
  22. /// <param name="timeout">请求的超时时间</param>
  23. /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
  24. /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
  25. /// <returns></returns>
  26. public static HttpWebResponse CreateGetHttpResponse(string url, int? timeout, string userAgent, CookieCollection cookies)
  27. {
  28. if ( string.IsNullOrEmpty(url) )
  29. {
  30. throw new ArgumentNullException("url");
  31. }
  32. HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
  33. request.Method = "GET";
  34. request.UserAgent = DefaultUserAgent;
  35. if ( !string.IsNullOrEmpty(userAgent) )
  36. {
  37. request.UserAgent = userAgent;
  38. }
  39. if ( timeout.HasValue )
  40. {
  41. request.Timeout = timeout.Value;
  42. }
  43. if ( cookies != null )
  44. {
  45. request.CookieContainer = new CookieContainer();
  46. request.CookieContainer.Add(cookies);
  47. }
  48. return request.GetResponse() as HttpWebResponse;
  49. }
  50. /// <summary>
  51. /// 创建POST方式的HTTP请求
  52. /// </summary>
  53. /// <param name="url">请求的URL</param>
  54. /// <param name="parameters">随同请求POST的参数名称及参数值字典</param>
  55. /// <param name="timeout">请求的超时时间</param>
  56. /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param>
  57. /// <param name="requestEncoding">发送HTTP请求时所用的编码</param>
  58. /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param>
  59. /// <returns></returns>
  60. public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieCollection cookies)
  61. {
  62. if ( string.IsNullOrEmpty(url) )
  63. {
  64. throw new ArgumentNullException("url");
  65. }
  66. if ( requestEncoding == null )
  67. {
  68. throw new ArgumentNullException("requestEncoding");
  69. }
  70. HttpWebRequest request=null;
  71. //如果是发送HTTPS请求
  72. if ( url.StartsWith("https", StringComparison.OrdinalIgnoreCase) )
  73. {
  74. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  75. request = WebRequest.Create(url) as HttpWebRequest;
  76. request.ProtocolVersion = HttpVersion.Version10;
  77. }
  78. else
  79. {
  80. request = WebRequest.Create(url) as HttpWebRequest;
  81. }
  82. request.Method = "POST";
  83. request.ContentType = "application/x-www-form-urlencoded";
  84. if ( !string.IsNullOrEmpty(userAgent) )
  85. {
  86. request.UserAgent = userAgent;
  87. }
  88. else
  89. {
  90. request.UserAgent = DefaultUserAgent;
  91. }
  92. if ( timeout.HasValue )
  93. {
  94. request.Timeout = timeout.Value;
  95. }
  96. if ( cookies != null )
  97. {
  98. request.CookieContainer = new CookieContainer();
  99. request.CookieContainer.Add(cookies);
  100. }
  101. //如果需要POST数据
  102. if ( !( parameters == null || parameters.Count == 0 ) )
  103. {
  104. StringBuilder buffer = new StringBuilder();
  105. int i = 0;
  106. foreach ( string key in parameters.Keys )
  107. {
  108. if ( i > 0 )
  109. {
  110. buffer.AppendFormat("&{0}={1}", key, parameters[ key ]);
  111. }
  112. else
  113. {
  114. buffer.AppendFormat("{0}={1}", key, parameters[ key ]);
  115. }
  116. i++;
  117. }
  118. byte[] data = requestEncoding.GetBytes(buffer.ToString());
  119. using ( Stream stream = request.GetRequestStream() )
  120. {
  121. stream.Write(data, 0, data.Length);
  122. }
  123. }
  124. return request.GetResponse() as HttpWebResponse;
  125. }
  126. private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  127. {
  128. return true; //总是接受
  129. }
  130. }
  131. }