diff --git a/Helper/Http/HttpClientHelper.cs b/Helper/Http/HttpClientHelper.cs index d3be226..56cb5c6 100644 --- a/Helper/Http/HttpClientHelper.cs +++ b/Helper/Http/HttpClientHelper.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Net.Http.Json; using System.Net.Security; using System.Security.Cryptography.X509Certificates; +using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Newtonsoft.Json; @@ -68,9 +69,9 @@ public class HttpClientHelper : IHttpHelper } } - public async Task PostAsync(string url, TIn input, int timeOut = 3) where TIn : class where TOut : class + + public async Task PostAsync(string url, TIn input, int timeOut = 3, CancellationToken? cts = null) where TIn : class where TOut : class { - _ = 2; try { HttpClient httpClient = GetHttpClient(); @@ -93,9 +94,8 @@ public class HttpClientHelper : IHttpHelper } } - public async Task GetAsync(string url, int timeOut = 10) + public async Task GetAsync(string url, int timeOut = 10,CancellationToken? cts = null) { - _ = 2; try { HttpClient httpClient = GetHttpClient(); @@ -117,7 +117,8 @@ public class HttpClientHelper : IHttpHelper } } - public bool DownloadFile(string url, string localPath) + + public bool DownloadFile(string url, string localPath) { ServicePointManager.ServerCertificateValidationCallback = (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true; HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest; diff --git a/Helper/Http/IHttpHelper.cs b/Helper/Http/IHttpHelper.cs index bcee35a..ddd1eda 100644 --- a/Helper/Http/IHttpHelper.cs +++ b/Helper/Http/IHttpHelper.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Net.Http; +using System.Threading; using System.Threading.Tasks; namespace Hua.DotNet.Code.Helper.Http; @@ -10,9 +11,9 @@ public interface IHttpHelper Dictionary Headers { get; set; } - Task PostAsync(string url, TIn input, int timeOut = 10) where TIn : class where TOut : class; + Task PostAsync(string url, TIn input, int timeOut = 10,CancellationToken? cts = null) where TIn : class where TOut : class; - Task GetAsync(string url, int timeOut = 10); + Task GetAsync(string url, int timeOut = 10, CancellationToken? cts = null); bool DownloadFile(string url, string fileFullName); } diff --git a/Helper/Http/RestClientHttpHelper.cs b/Helper/Http/RestClientHttpHelper.cs index e9a0094..11c510a 100644 --- a/Helper/Http/RestClientHttpHelper.cs +++ b/Helper/Http/RestClientHttpHelper.cs @@ -21,32 +21,29 @@ public class RestClientHttpHelper : IHttpHelper _logger = logger; } - public virtual RestClient GetHttpClient() + protected virtual RestClient GetHttpClient() { - //IL_0016: Unknown result type (might be due to invalid IL or missing references) - //IL_000f: Unknown result type (might be due to invalid IL or missing references) - //IL_001c: Expected O, but got Unknown - RestClient val = ((Handler == null) ? new RestClient() : new RestClient((HttpMessageHandler)Handler, true)); + var val = ((Handler == null) ? new RestClient() : new RestClient((HttpMessageHandler)Handler, true)); if (Headers == null) { return val; } foreach (KeyValuePair header in Headers) { - RestClientExtensions.AddDefaultHeader(val, header.Key, header.Value); + val.AddDefaultHeader(header.Key, header.Value); } return val; } - public async Task PostAsync(string url, TIn input, int timeOut = 10) where TIn : class where TOut : class + public async Task PostAsync(string url, TIn input, int timeOut = 10, CancellationToken? cts = null) where TIn : class where TOut : class { try { - RestClient client = GetHttpClient(); - RestRequest request = RestRequestExtensions.AddJsonBody(new RestRequest(url, (Method)0), input, "application/json"); + var client = GetHttpClient(); + var request = new RestRequest(url, Method.Post).AddJsonBody(input, "application/json"); request.Timeout = TimeSpan.FromSeconds(timeOut); - await RestClientExtensions.PostAsync(client, request, CancellationToken.None); - return await RestClientExtensions.PostAsync(client, request, default(CancellationToken)); + await client.PostAsync(request, cts ?? CancellationToken.None); + return await client.PostAsync(request, cts??CancellationToken.None); } catch (Exception ex) { @@ -56,13 +53,13 @@ public class RestClientHttpHelper : IHttpHelper } } - public async Task GetAsync(string url, int timeOut = 10) + public async Task GetAsync(string url, int timeOut = 10, CancellationToken? cts = null) { try { - RestClient httpClient = GetHttpClient(); - RestRequest val = new RestRequest(url, (Method)0); - return await RestClientExtensions.GetAsync(httpClient, val, default(CancellationToken)); + using RestClient httpClient = GetHttpClient(); + var val = new RestRequest(url,Method.Get); + return await httpClient.GetAsync(val, cts ?? CancellationToken.None); } catch (Exception ex) { @@ -72,7 +69,8 @@ public class RestClientHttpHelper : IHttpHelper } } - public bool DownloadFile(string url, string fileFullName) + + public bool DownloadFile(string url, string fileFullName) { throw new NotImplementedException(); }