|
@@ -10,142 +10,142 @@ using System.Web;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers.Third.Xkw
|
|
|
{
|
|
|
- public class XopHttpClient : IDisposable
|
|
|
- {
|
|
|
- private static int DEFAULT_TIMEOUT = 5;
|
|
|
-
|
|
|
- private String baseUrl;
|
|
|
- private String appId;
|
|
|
- private String secret;
|
|
|
-
|
|
|
- public int TimeoutInSeconds { get; set; } = DEFAULT_TIMEOUT;
|
|
|
-
|
|
|
- private HttpClient httpClient;
|
|
|
-
|
|
|
-
|
|
|
- public void Dispose()
|
|
|
- {
|
|
|
- this.httpClient?.Dispose();
|
|
|
- }
|
|
|
-
|
|
|
- public XopHttpClient(String baseUrl, String appId, String secret) : this(baseUrl, appId, secret, null)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- public XopHttpClient(String baseUrl, String appId, String secret, WebProxy proxy) : this(baseUrl, appId, secret, DEFAULT_TIMEOUT, proxy)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- public XopHttpClient(String baseUrl, String appId, String secret, int timeout) : this(baseUrl, appId, secret, timeout, null)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- public XopHttpClient(String baseUrl, String appId, String secret, int timeout, WebProxy proxy)
|
|
|
- {
|
|
|
- this.baseUrl = baseUrl;
|
|
|
- this.appId = appId;
|
|
|
- this.secret = secret;
|
|
|
- if (proxy != null)
|
|
|
- {
|
|
|
- var httpClientHandler = new HttpClientHandler
|
|
|
- {
|
|
|
- Proxy = proxy
|
|
|
- };
|
|
|
- this.httpClient = new HttpClient(httpClientHandler, true);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- this.httpClient = new HttpClient();
|
|
|
- }
|
|
|
- this.httpClient.DefaultRequestHeaders.Add(HmacConst.KEY_APP_ID, appId);
|
|
|
- }
|
|
|
-
|
|
|
- public T Get<T>(String uri)
|
|
|
- {
|
|
|
- return Get<T>(uri, null);
|
|
|
- }
|
|
|
-
|
|
|
- public T Get<T>(String uri, Dictionary<String, String> parameters)
|
|
|
- {
|
|
|
- return SendRequest<T>(HttpMethod.Get, uri, parameters, null, null, this.TimeoutInSeconds);
|
|
|
- }
|
|
|
-
|
|
|
- public T Post<T>(String uri, Dictionary<String, String> parameters, String text, String contentType)
|
|
|
- {
|
|
|
- return SendRequest<T>(HttpMethod.Post, uri, parameters, text, "application/json", this.TimeoutInSeconds);
|
|
|
- }
|
|
|
-
|
|
|
- public T PostAsText<T>(String uri, Dictionary<String, String> parameters, String text)
|
|
|
- {
|
|
|
- return SendRequest<T>(HttpMethod.Post, uri, parameters, text, "text/plain", this.TimeoutInSeconds);
|
|
|
- }
|
|
|
-
|
|
|
- public T PostAsJson<T>(String uri, Dictionary<String, String> parameters, Object data)
|
|
|
- {
|
|
|
+ //public class XopHttpClient : IDisposable
|
|
|
+ //{
|
|
|
+ // private static int DEFAULT_TIMEOUT = 5;
|
|
|
+
|
|
|
+ // private String baseUrl;
|
|
|
+ // private String appId;
|
|
|
+ // private String secret;
|
|
|
+
|
|
|
+ // public int TimeoutInSeconds { get; set; } = DEFAULT_TIMEOUT;
|
|
|
+
|
|
|
+ // private IHttpClientFactory httpClient;
|
|
|
+
|
|
|
+
|
|
|
+ // public void Dispose()
|
|
|
+ // {
|
|
|
+ // this.httpClient?.Dispose();
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public XopHttpClient(String baseUrl, String appId, String secret) : this(baseUrl, appId, secret, null)
|
|
|
+ // {
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public XopHttpClient(String baseUrl, String appId, String secret, WebProxy proxy) : this(baseUrl, appId, secret, DEFAULT_TIMEOUT, proxy)
|
|
|
+ // {
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public XopHttpClient(String baseUrl, String appId, String secret, int timeout) : this(baseUrl, appId, secret, timeout, null)
|
|
|
+ // {
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public XopHttpClient(String baseUrl, String appId, String secret, int timeout, WebProxy proxy)
|
|
|
+ // {
|
|
|
+ // this.baseUrl = baseUrl;
|
|
|
+ // this.appId = appId;
|
|
|
+ // this.secret = secret;
|
|
|
+ // if (proxy != null)
|
|
|
+ // {
|
|
|
+ // var httpClientHandler = new HttpClientHandler
|
|
|
+ // {
|
|
|
+ // Proxy = proxy
|
|
|
+ // };
|
|
|
+ // this.httpClient = new HttpClient(httpClientHandler, true);
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // this.httpClient = new HttpClient();
|
|
|
+ // }
|
|
|
+ // this.httpClient.DefaultRequestHeaders.Add(HmacConst.KEY_APP_ID, appId);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public T Get<T>(String uri)
|
|
|
+ // {
|
|
|
+ // return Get<T>(uri, null);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public T Get<T>(String uri, Dictionary<String, String> parameters)
|
|
|
+ // {
|
|
|
+ // return SendRequest<T>(HttpMethod.Get, uri, parameters, null, null, this.TimeoutInSeconds);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public T Post<T>(String uri, Dictionary<String, String> parameters, String text, String contentType)
|
|
|
+ // {
|
|
|
+ // return SendRequest<T>(HttpMethod.Post, uri, parameters, text, "application/json", this.TimeoutInSeconds);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public T PostAsText<T>(String uri, Dictionary<String, String> parameters, String text)
|
|
|
+ // {
|
|
|
+ // return SendRequest<T>(HttpMethod.Post, uri, parameters, text, "text/plain", this.TimeoutInSeconds);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // public T PostAsJson<T>(String uri, Dictionary<String, String> parameters, Object data)
|
|
|
+ // {
|
|
|
|
|
|
- string json = JsonConvert.SerializeObject(data);
|
|
|
- return SendRequest<T>(HttpMethod.Post, uri, parameters, json, "application/json", this.TimeoutInSeconds);
|
|
|
- }
|
|
|
-
|
|
|
- //public T PostAsForm<T>(String uri, Dictionary<String, String> parameters, Dictionary<String, String> formData)
|
|
|
- //{
|
|
|
- // if (formData == null)
|
|
|
- // {
|
|
|
- // formData = new Dictionary<string, string>();
|
|
|
- // }
|
|
|
- // using (HttpContent requestContent = new FormUrlEncodedContent(formData.ToList()))
|
|
|
- // {
|
|
|
- // return SendRequest<T>(HttpMethod.Post, uri, parameters, requestContent, this.TimeoutInSeconds);
|
|
|
- // }
|
|
|
- //}
|
|
|
-
|
|
|
- public T SendRequest<T>(HttpMethod method, String path, Dictionary<String, String> parameters, String requestBodyStr, String requestContentType, int timeoutInSeconds)
|
|
|
- {
|
|
|
- Dictionary<String, String> dic = new Dictionary<string, string>();
|
|
|
- if (parameters != null)
|
|
|
- {
|
|
|
- foreach (var item in parameters)
|
|
|
- {
|
|
|
- dic.Add(item.Key, item.Value);
|
|
|
- }
|
|
|
- }
|
|
|
- // 不对body验签,添加url验签
|
|
|
- //UriIdentifier uriIdentifier = HmacCommonUtils.SplitUriPrefix(path);
|
|
|
- dic.Add(HmacConst.KEY_URL, path);
|
|
|
- HmacResult signRlt = HmacUtils.Sign(appId, secret, dic, requestBodyStr);
|
|
|
-
|
|
|
- String pathWithQuery = path;
|
|
|
- if (parameters?.Count > 0)
|
|
|
- {
|
|
|
- pathWithQuery = path + "?" + String.Join("&", parameters.Select(i => $"{i.Key}={HttpUtility.UrlEncode(i.Value)}"));
|
|
|
- }
|
|
|
-
|
|
|
- HttpRequestMessage request = new HttpRequestMessage(method, baseUrl + pathWithQuery);
|
|
|
- if (requestBodyStr != null)
|
|
|
- {
|
|
|
- StringContent requestContent = new StringContent(requestBodyStr, Encoding.UTF8, requestContentType);
|
|
|
- request.Content = requestContent;
|
|
|
- }
|
|
|
- request.Headers.Add(HmacConst.KEY_TIMESTAMP, signRlt.TimeStamp.ToString());
|
|
|
- request.Headers.Add(HmacConst.KEY_SIGN, signRlt.Sign);
|
|
|
- request.Headers.Add(HmacConst.KEY_NONCE, signRlt.Nonce);
|
|
|
-
|
|
|
- HttpResponseMessage response = null;
|
|
|
- using (CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds)))
|
|
|
- {
|
|
|
- response = this.httpClient.SendAsync(request, cts.Token).Result;
|
|
|
- }
|
|
|
- String responseText = response.Content.ReadAsStringAsync().Result;
|
|
|
- if (response.StatusCode != HttpStatusCode.OK)
|
|
|
- {
|
|
|
- String message = $"服务错误: 状态码: {(int)response.StatusCode}, resposneText: {responseText}";
|
|
|
- XopException bapiException = new XopException(message, response);
|
|
|
- throw bapiException;
|
|
|
- }
|
|
|
-
|
|
|
- return JsonConvert.DeserializeObject<T>(responseText);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
+ // string json = JsonConvert.SerializeObject(data);
|
|
|
+ // return SendRequest<T>(HttpMethod.Post, uri, parameters, json, "application/json", this.TimeoutInSeconds);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // //public T PostAsForm<T>(String uri, Dictionary<String, String> parameters, Dictionary<String, String> formData)
|
|
|
+ // //{
|
|
|
+ // // if (formData == null)
|
|
|
+ // // {
|
|
|
+ // // formData = new Dictionary<string, string>();
|
|
|
+ // // }
|
|
|
+ // // using (HttpContent requestContent = new FormUrlEncodedContent(formData.ToList()))
|
|
|
+ // // {
|
|
|
+ // // return SendRequest<T>(HttpMethod.Post, uri, parameters, requestContent, this.TimeoutInSeconds);
|
|
|
+ // // }
|
|
|
+ // //}
|
|
|
+
|
|
|
+ // public T SendRequest<T>(HttpMethod method, String path, Dictionary<String, String> parameters, String requestBodyStr, String requestContentType, int timeoutInSeconds)
|
|
|
+ // {
|
|
|
+ // Dictionary<String, String> dic = new Dictionary<string, string>();
|
|
|
+ // if (parameters != null)
|
|
|
+ // {
|
|
|
+ // foreach (var item in parameters)
|
|
|
+ // {
|
|
|
+ // dic.Add(item.Key, item.Value);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // // 不对body验签,添加url验签
|
|
|
+ // //UriIdentifier uriIdentifier = HmacCommonUtils.SplitUriPrefix(path);
|
|
|
+ // dic.Add(HmacConst.KEY_URL, path);
|
|
|
+ // HmacResult signRlt = HmacUtils.Sign(appId, secret, dic, requestBodyStr);
|
|
|
+
|
|
|
+ // String pathWithQuery = path;
|
|
|
+ // if (parameters?.Count > 0)
|
|
|
+ // {
|
|
|
+ // pathWithQuery = path + "?" + String.Join("&", parameters.Select(i => $"{i.Key}={HttpUtility.UrlEncode(i.Value)}"));
|
|
|
+ // }
|
|
|
+
|
|
|
+ // HttpRequestMessage request = new HttpRequestMessage(method, baseUrl + pathWithQuery);
|
|
|
+ // if (requestBodyStr != null)
|
|
|
+ // {
|
|
|
+ // StringContent requestContent = new StringContent(requestBodyStr, Encoding.UTF8, requestContentType);
|
|
|
+ // request.Content = requestContent;
|
|
|
+ // }
|
|
|
+ // request.Headers.Add(HmacConst.KEY_TIMESTAMP, signRlt.TimeStamp.ToString());
|
|
|
+ // request.Headers.Add(HmacConst.KEY_SIGN, signRlt.Sign);
|
|
|
+ // request.Headers.Add(HmacConst.KEY_NONCE, signRlt.Nonce);
|
|
|
+
|
|
|
+ // HttpResponseMessage response = null;
|
|
|
+ // using (CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutInSeconds)))
|
|
|
+ // {
|
|
|
+ // response = this.httpClient.SendAsync(request, cts.Token).Result;
|
|
|
+ // }
|
|
|
+ // String responseText = response.Content.ReadAsStringAsync().Result;
|
|
|
+ // if (response.StatusCode != HttpStatusCode.OK)
|
|
|
+ // {
|
|
|
+ // String message = $"服务错误: 状态码: {(int)response.StatusCode}, resposneText: {responseText}";
|
|
|
+ // XopException bapiException = new XopException(message, response);
|
|
|
+ // throw bapiException;
|
|
|
+ // }
|
|
|
+
|
|
|
+ // return JsonConvert.DeserializeObject<T>(responseText);
|
|
|
+ // }
|
|
|
+
|
|
|
+ //}
|
|
|
|
|
|
}
|