浏览代码

HttpClientUtility => HttpClientExtensions

Roc 6 年之前
父节点
当前提交
69aa67a1a8

+ 3 - 3
src/Essensoft.AspNetCore.Payment.Alipay/AlipayClient.cs

@@ -131,7 +131,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay
 
                 using (var client = ClientFactory.CreateClient())
                 {
-                    body = await HttpClientUtility.DoPostAsync(client, options.ServerUrl, txtParams, fileParams);
+                    body = await client.DoPostAsync(options.ServerUrl, txtParams, fileParams);
                 }
             }
             else
@@ -251,11 +251,11 @@ namespace Essensoft.AspNetCore.Payment.Alipay
                 {
                     var fileParams = AlipayUtility.CleanupDictionary(uRequest.GetFileParameters());
 
-                    body = await HttpClientUtility.DoPostAsync(client, options.ServerUrl, txtParams, fileParams);
+                    body = await client.DoPostAsync(options.ServerUrl, txtParams, fileParams);
                 }
                 else
                 {
-                    body = await HttpClientUtility.DoPostAsync(client, options.ServerUrl, query);
+                    body = await client.DoPostAsync(options.ServerUrl, query);
                 }
             }
 

+ 6 - 6
src/Essensoft.AspNetCore.Payment.Alipay/Utility/HttpClientUtility.cs → src/Essensoft.AspNetCore.Payment.Alipay/Utility/HttpClientExtensions.cs

@@ -8,9 +8,9 @@ using System.Threading.Tasks;
 namespace Essensoft.AspNetCore.Payment.Alipay.Utility
 {
     /// <summary>
-    /// 网络工具类
+    /// HTTP客户端扩展
     /// </summary>
-    public static class HttpClientUtility
+    public static class HttpClientExtensions
     {
         /// <summary>
         /// 执行HTTP POST请求。
@@ -18,7 +18,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay.Utility
         /// <param name="url">请求地址</param>
         /// <param name="parameters">请求参数</param>
         /// <returns>HTTP响应</returns>
-        public static async Task<string> DoPostAsync(HttpClient client, string url, IDictionary<string, string> parameters)
+        public static async Task<string> DoPostAsync(this HttpClient client, string url, IDictionary<string, string> parameters)
         {
             using (var requestContent = new StringContent(AlipayUtility.BuildQuery(parameters), Encoding.UTF8, "application/x-www-form-urlencoded"))
             using (var response = await client.PostAsync(url, requestContent))
@@ -34,7 +34,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay.Utility
         /// <param name="url">请求地址</param>
         /// <param name="content">请求内容</param>
         /// <returns>HTTP响应</returns>
-        public static async Task<string> DoPostAsync(HttpClient client, string url, string content)
+        public static async Task<string> DoPostAsync(this HttpClient client, string url, string content)
         {
             using (var requestContent = new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded"))
             using (var response = await client.PostAsync(url, requestContent))
@@ -50,7 +50,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay.Utility
         /// <param name="url">请求地址</param>
         /// <param name="parameters">请求参数</param>
         /// <returns>HTTP响应</returns>
-        public static async Task<string> DoGetAsync(HttpClient client, string url, IDictionary<string, string> parameters)
+        public static async Task<string> DoGetAsync(this HttpClient client, string url, IDictionary<string, string> parameters)
         {
             if (parameters?.Count > 0)
             {
@@ -78,7 +78,7 @@ namespace Essensoft.AspNetCore.Payment.Alipay.Utility
         /// <param name="textParams">请求文本参数</param>
         /// <param name="fileParams">请求文件参数</param>
         /// <returns>HTTP响应</returns>
-        public static async Task<string> DoPostAsync(HttpClient client, string url, IDictionary<string, string> textParams, IDictionary<string, FileItem> fileParams)
+        public static async Task<string> DoPostAsync(this HttpClient client, string url, IDictionary<string, string> textParams, IDictionary<string, FileItem> fileParams)
         {
             // 如果没有文件参数,则走普通POST请求
             if (fileParams == null || fileParams.Count == 0)

+ 2 - 2
src/Essensoft.AspNetCore.Payment.JDPay/JDPayClient.cs

@@ -54,7 +54,7 @@ namespace Essensoft.AspNetCore.Payment.JDPay
 
             using (var client = ClientFactory.CreateClient())
             {
-                var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content);
+                var body = await client.DoPostAsync(request.GetRequestUrl(), content);
                 Logger.Log(options.LogLevel, "Response:{content}", body);
 
                 var parser = new JDPayXmlParser<T>();
@@ -151,7 +151,7 @@ namespace Essensoft.AspNetCore.Payment.JDPay
 
             using (var client = ClientFactory.CreateClient())
             {
-                var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content, "application/x-www-form-urlencoded");
+                var body = await client.DoPostAsync(request.GetRequestUrl(), content, "application/x-www-form-urlencoded");
                 Logger.Log(options.LogLevel, "Response:{content}", body);
 
                 var rsp = JsonConvert.DeserializeObject<T>(body);

+ 5 - 2
src/Essensoft.AspNetCore.Payment.JDPay/Utility/HttpClientUtility.cs → src/Essensoft.AspNetCore.Payment.JDPay/Utility/HttpClientExtensions.cs

@@ -4,7 +4,10 @@ using System.Threading.Tasks;
 
 namespace Essensoft.AspNetCore.Payment.JDPay.Utility
 {
-    public static class HttpClientUtility
+    /// <summary>
+    /// HTTP客户端扩展。
+    /// </summary>
+    public static class HttpClientExtensions
     {
         /// <summary>
         /// 执行HTTP POST请求。
@@ -12,7 +15,7 @@ namespace Essensoft.AspNetCore.Payment.JDPay.Utility
         /// <param name="url">请求地址</param>
         /// <param name="content">请求内容</param>
         /// <returns>HTTP响应</returns>
-        public static async Task<string> DoPostAsync(HttpClient client, string url, string content, string mediaType = "application/xml")
+        public static async Task<string> DoPostAsync(this HttpClient client, string url, string content, string mediaType = "application/xml")
         {
             using (var requestContent = new StringContent(content, Encoding.UTF8, mediaType))
             using (var response = await client.PostAsync(url, requestContent))

+ 1 - 1
src/Essensoft.AspNetCore.Payment.LianLianPay/LianLianPayClient.cs

@@ -83,7 +83,7 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay
 
             using (var client = ClientFactory.CreateClient())
             {
-                var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content);
+                var body = await client.DoPostAsync(request.GetRequestUrl(), content);
                 Logger.Log(options.LogLevel, "Response:{body}", body);
 
                 var parser = new LianLianPayJsonParser<T>();

+ 3 - 3
src/Essensoft.AspNetCore.Payment.LianLianPay/Utility/HttpClientUtility.cs → src/Essensoft.AspNetCore.Payment.LianLianPay/Utility/HttpClientExtensions.cs

@@ -5,9 +5,9 @@ using System.Threading.Tasks;
 namespace Essensoft.AspNetCore.Payment.LianLianPay.Utility
 {
     /// <summary>
-    /// 网络工具类
+    /// HTTP客户端扩展
     /// </summary>
-    public static class HttpClientUtility
+    public static class HttpClientExtensions
     {
         /// <summary>
         /// 执行HTTP POST请求。
@@ -15,7 +15,7 @@ namespace Essensoft.AspNetCore.Payment.LianLianPay.Utility
         /// <param name="url">请求地址</param>
         /// <param name="content">请求内容</param>
         /// <returns>HTTP响应</returns>
-        public static async Task<string> DoPostAsync(HttpClient client, string url, string content)
+        public static async Task<string> DoPostAsync(this HttpClient client, string url, string content)
         {
             using (var requestContent = new StringContent(content, Encoding.UTF8, "application/json"))
             using (var response = await client.PostAsync(url, requestContent))

+ 26 - 26
src/Essensoft.AspNetCore.Payment.QPay/QPayClient.cs

@@ -35,30 +35,6 @@ namespace Essensoft.AspNetCore.Payment.QPay
 
         public IOptionsSnapshot<QPayOptions> OptionsSnapshotAccessor { get; set; }
 
-        #region Common Method
-
-        private void CheckResponseSign(QPayResponse response, QPayOptions options)
-        {
-            if (string.IsNullOrEmpty(response.Body) || response?.Parameters == null)
-            {
-                throw new Exception("sign check fail: Body is Empty!");
-            }
-
-            if (response.Parameters.TryGetValue("sign", out var sign))
-            {
-                if (response.Parameters["return_code"] == "SUCCESS" && !string.IsNullOrEmpty(sign))
-                {
-                    var cal_sign = QPaySignature.SignWithKey(response.Parameters, options.Key);
-                    if (cal_sign != sign)
-                    {
-                        throw new Exception("sign check fail: check Sign and Data Fail!");
-                    }
-                }
-            }
-        }
-
-        #endregion
-
         #region IQPayClient Members
 
         public async Task<T> ExecuteAsync<T>(IQPayRequest<T> request) where T : QPayResponse
@@ -88,7 +64,7 @@ namespace Essensoft.AspNetCore.Payment.QPay
 
             using (var client = ClientFactory.CreateClient())
             {
-                var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content);
+                var body = await client.DoPostAsync(request.GetRequestUrl(), content);
                 Logger.Log(options.LogLevel, "Response:{body}", body);
 
                 var parser = new QPayXmlParser<T>();
@@ -127,7 +103,7 @@ namespace Essensoft.AspNetCore.Payment.QPay
             Logger.Log(options.LogLevel, "Request:{content}", content);
             using (var client = string.IsNullOrEmpty(certificateName) ? ClientFactory.CreateClient() : ClientFactory.CreateClient(certificateName))
             {
-                var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content);
+                var body = await client.DoPostAsync(request.GetRequestUrl(), content);
                 Logger.Log(options.LogLevel, "Response:{body}", body);
 
                 var parser = new QPayXmlParser<T>();
@@ -138,5 +114,29 @@ namespace Essensoft.AspNetCore.Payment.QPay
         }
 
         #endregion
+
+        #region Common Method
+
+        private void CheckResponseSign(QPayResponse response, QPayOptions options)
+        {
+            if (string.IsNullOrEmpty(response.Body) || response?.Parameters == null)
+            {
+                throw new Exception("sign check fail: Body is Empty!");
+            }
+
+            if (response.Parameters.TryGetValue("sign", out var sign))
+            {
+                if (response.Parameters["return_code"] == "SUCCESS" && !string.IsNullOrEmpty(sign))
+                {
+                    var cal_sign = QPaySignature.SignWithKey(response.Parameters, options.Key);
+                    if (cal_sign != sign)
+                    {
+                        throw new Exception("sign check fail: check Sign and Data Fail!");
+                    }
+                }
+            }
+        }
+
+        #endregion
     }
 }

+ 5 - 2
src/Essensoft.AspNetCore.Payment.QPay/Utility/HttpClientUtility.cs → src/Essensoft.AspNetCore.Payment.QPay/Utility/HttpClientExtensions.cs

@@ -4,7 +4,10 @@ using System.Threading.Tasks;
 
 namespace Essensoft.AspNetCore.Payment.QPay.Utility
 {
-    public static class HttpClientUtility
+    /// <summary>
+    /// HTTP客户端扩展。
+    /// </summary>
+    public static class HttpClientExtensions
     {
         /// <summary>
         /// 执行HTTP POST请求。
@@ -12,7 +15,7 @@ namespace Essensoft.AspNetCore.Payment.QPay.Utility
         /// <param name="url">请求地址</param>
         /// <param name="content">请求参数</param>
         /// <returns>HTTP响应</returns>
-        public static async Task<string> DoPostAsync(HttpClient client, string url, string content)
+        public static async Task<string> DoPostAsync(this HttpClient client, string url, string content)
         {
             using (var requestContent = new StringContent(content, Encoding.UTF8, "application/xml"))
             using (var response = await client.PostAsync(url, requestContent))

+ 5 - 2
src/Essensoft.AspNetCore.Payment.WeChatPay/Utility/HttpClientUtility.cs → src/Essensoft.AspNetCore.Payment.WeChatPay/Utility/HttpClientExtensions.cs

@@ -4,7 +4,10 @@ using System.Threading.Tasks;
 
 namespace Essensoft.AspNetCore.Payment.WeChatPay.Utility
 {
-    public static class HttpClientUtility
+    /// <summary>
+    /// HTTP客户端扩展。
+    /// </summary>
+    public static class HttpClientExtensions
     {
         /// <summary>
         /// 执行HTTP POST请求。
@@ -12,7 +15,7 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay.Utility
         /// <param name="url">请求地址</param>
         /// <param name="content">请求参数</param>
         /// <returns>HTTP响应</returns>
-        public static async Task<string> DoPostAsync(HttpClient client, string url, string content)
+        public static async Task<string> DoPostAsync(this HttpClient client, string url, string content)
         {
             using (var requestContent = new StringContent(content, Encoding.UTF8, "application/xml"))
             using (var response = await client.PostAsync(url, requestContent))

+ 2 - 2
src/Essensoft.AspNetCore.Payment.WeChatPay/WeChatPayClient.cs

@@ -79,7 +79,7 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay
 
             using (var client = ClientFactory.CreateClient())
             {
-                var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content);
+                var body = await client.DoPostAsync(request.GetRequestUrl(), content);
                 Logger.Log(options.LogLevel, "Response:{body}", body);
 
                 var parser = new WeChatPayXmlParser<T>();
@@ -197,7 +197,7 @@ namespace Essensoft.AspNetCore.Payment.WeChatPay
 
             using (var client = ClientFactory.CreateClient(certificateName))
             {
-                var body = await HttpClientUtility.DoPostAsync(client, request.GetRequestUrl(), content);
+                var body = await client.DoPostAsync(request.GetRequestUrl(), content);
 
                 Logger.Log(options.LogLevel, "Response:{body}", body);