HttpWebResponseUtility.cs 5.3 KB

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