HttpService.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using Gateway.Payment.Shared;
  2. using System;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Security;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Text;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Logging;
  10. namespace Wechat.PayAPI
  11. {
  12. /// <summary>
  13. /// http连接基础类,负责底层的http通信
  14. /// </summary>
  15. public class HttpService
  16. {
  17. public static ILogger Log = Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance;
  18. public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  19. {
  20. //直接确认,否则打不开
  21. return true;
  22. }
  23. public static string Post(string xml, string url, WxPayConfig config, X509Certificate2 certification, int timeout)
  24. {
  25. string result = "";//返回结果
  26. HttpWebRequest request = null;
  27. HttpWebResponse response = null;
  28. Stream reqStream = null;
  29. try
  30. {
  31. //设置最大连接数
  32. ServicePointManager.DefaultConnectionLimit = 200;
  33. //设置https验证方式
  34. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  35. {
  36. ServicePointManager.ServerCertificateValidationCallback =
  37. new RemoteCertificateValidationCallback(CheckValidationResult);
  38. }
  39. /***************************************************************
  40. * 下面设置HttpWebRequest的相关属性
  41. * ************************************************************/
  42. request = (HttpWebRequest)WebRequest.Create(url);
  43. request.Method = "POST";
  44. request.Timeout = timeout * 1000;
  45. ////设置代理服务器
  46. //WebProxy proxy = new WebProxy(); //定义一个网关对象
  47. //proxy.Address = new Uri(WxPayConfig.PROXY_URL); //网关服务器端口:端口
  48. //request.Proxy = proxy;
  49. //设置POST的数据类型和长度
  50. request.ContentType = "text/xml";
  51. byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
  52. request.ContentLength = data.Length;
  53. //是否使用证书
  54. if (certification != null)
  55. {
  56. //Abby
  57. //X509Certificate2 cert = new X509Certificate2(config.SSLCERT, config.SSLCERT_PASSWORD);
  58. request.ClientCertificates.Add(certification);
  59. Log.LogDebug("WxPayApi", "PostXml used cert");
  60. }
  61. //往服务器写入数据
  62. reqStream = request.GetRequestStream();
  63. reqStream.Write(data, 0, data.Length);
  64. reqStream.Close();
  65. //获取服务端返回
  66. response = (HttpWebResponse)request.GetResponse();
  67. //获取服务端返回数据
  68. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  69. result = sr.ReadToEnd().Trim();
  70. sr.Close();
  71. }
  72. catch (System.Threading.ThreadAbortException e)
  73. {
  74. Log.LogError("HttpService", "Thread - caught ThreadAbortException - resetting.");
  75. Log.LogError("Exception message: {0}", e.Message);
  76. System.Threading.Thread.ResetAbort();
  77. }
  78. catch (WebException e)
  79. {
  80. Log.LogError("HttpService", e.ToString());
  81. if (e.Status == WebExceptionStatus.ProtocolError)
  82. {
  83. Log.LogError("HttpService", "StatusCode : " + ((HttpWebResponse)e.Response).StatusCode);
  84. Log.LogError("HttpService", "StatusDescription : " + ((HttpWebResponse)e.Response).StatusDescription);
  85. }
  86. throw new WxPayException(e.ToString());
  87. }
  88. catch (Exception e)
  89. {
  90. Log.LogError("HttpService", e.ToString());
  91. throw new WxPayException(e.ToString());
  92. }
  93. finally
  94. {
  95. //关闭连接和流
  96. if (response != null)
  97. {
  98. response.Close();
  99. }
  100. if(request != null)
  101. {
  102. request.Abort();
  103. }
  104. }
  105. return result;
  106. }
  107. /// <summary>
  108. /// 处理http GET请求,返回数据
  109. /// </summary>
  110. /// <param name="url">请求的url地址</param>
  111. /// <returns>http GET成功后返回的数据,失败抛WebException异常</returns>
  112. public static string Get(string url, Microsoft.Extensions.Logging.ILogger Log)
  113. {
  114. string result = "";
  115. HttpWebRequest request = null;
  116. HttpWebResponse response = null;
  117. //请求url以获取数据
  118. try
  119. {
  120. //设置最大连接数
  121. ServicePointManager.DefaultConnectionLimit = 200;
  122. //设置https验证方式
  123. if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
  124. {
  125. ServicePointManager.ServerCertificateValidationCallback =
  126. new RemoteCertificateValidationCallback(CheckValidationResult);
  127. }
  128. /***************************************************************
  129. * 下面设置HttpWebRequest的相关属性
  130. * ************************************************************/
  131. request = (HttpWebRequest)WebRequest.Create(url);
  132. request.Method = "GET";
  133. ////设置代理
  134. //WebProxy proxy = new WebProxy();
  135. //proxy.Address = new Uri(WxPayConfig.PROXY_URL);
  136. //request.Proxy = proxy;
  137. //获取服务器返回
  138. response = (HttpWebResponse)request.GetResponse();
  139. //获取HTTP返回数据
  140. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  141. result = sr.ReadToEnd().Trim();
  142. sr.Close();
  143. }
  144. catch (System.Threading.ThreadAbortException e)
  145. {
  146. Log.LogError("HttpService","Thread - caught ThreadAbortException - resetting.");
  147. Log.LogError("Exception message: {0}", e.Message);
  148. System.Threading.Thread.ResetAbort();
  149. }
  150. catch (WebException e)
  151. {
  152. Log.LogError("HttpService", e.ToString());
  153. if (e.Status == WebExceptionStatus.ProtocolError)
  154. {
  155. Log.LogError("HttpService", "StatusCode : " + ((HttpWebResponse)e.Response).StatusCode);
  156. Log.LogError("HttpService", "StatusDescription : " + ((HttpWebResponse)e.Response).StatusDescription);
  157. }
  158. throw new WxPayException(e.ToString());
  159. }
  160. catch (Exception e)
  161. {
  162. Log.LogError("HttpService", e.ToString());
  163. throw new WxPayException(e.ToString());
  164. }
  165. finally
  166. {
  167. //关闭连接和流
  168. if (response != null)
  169. {
  170. response.Close();
  171. }
  172. if (request != null)
  173. {
  174. request.Abort();
  175. }
  176. }
  177. return result;
  178. }
  179. }
  180. }