using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; namespace HT.Cloud.Code { public class HttpWebClient { private IHttpClientFactory _httpClientFactory; public HttpWebClient(IHttpClientFactory httpClientFactory) { this._httpClientFactory = httpClientFactory; } /// /// Get异步请求 /// /// /// /// /// public async Task GetAsync(string url, Dictionary dicHeaders, int timeoutSecond = 120) { var client = _httpClientFactory.CreateClient(); var request = new HttpRequestMessage(HttpMethod.Get, url); if (dicHeaders != null) { foreach (var header in dicHeaders) { request.Headers.Add(header.Key, header.Value); } } client.Timeout = TimeSpan.FromSeconds(timeoutSecond); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return result; } else { throw new CustomerHttpException($"接口请求错误,错误代码{response.StatusCode},错误原因{response.ReasonPhrase}"); } } /// /// /// /// /// /// /// /// public async Task PostAsync(string url, string requestString, Dictionary dicHeaders, int timeoutSecond) { var client = _httpClientFactory.CreateClient(); var requestContent = new StringContent(requestString); if (dicHeaders != null) { foreach (var head in dicHeaders) { requestContent.Headers.Add(head.Key, head.Value); } } client.Timeout = TimeSpan.FromSeconds(timeoutSecond); var response = await client.PostAsync(url, requestContent); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return result; } else { throw new CustomerHttpException($"接口请求错误,错误代码{response.StatusCode},错误原因{response.ReasonPhrase}"); } } /// /// /// /// /// /// /// /// public async Task PutAsync(string url, string requestString, Dictionary dicHeaders, int timeoutSecond) { var client = _httpClientFactory.CreateClient(); var requestContent = new StringContent(requestString); if (dicHeaders != null) { foreach (var head in dicHeaders) { requestContent.Headers.Add(head.Key, head.Value); } } client.Timeout = TimeSpan.FromSeconds(timeoutSecond); var response = await client.PutAsync(url, requestContent); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return result; } else { throw new CustomerHttpException($"接口请求错误,错误代码{response.StatusCode},错误原因{response.ReasonPhrase}"); } } /// /// Patch异步请求 /// /// /// /// /// /// public async Task PatchAsync(string url, string requestString, Dictionary dicHeaders, int timeoutSecond) { var client = _httpClientFactory.CreateClient(); var requestContent = new StringContent(requestString); if (dicHeaders != null) { foreach (var head in dicHeaders) { requestContent.Headers.Add(head.Key, head.Value); } } client.Timeout = TimeSpan.FromSeconds(timeoutSecond); var response = await client.PatchAsync(url, requestContent); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return result; } else { throw new CustomerHttpException($"接口请求错误,错误代码{response.StatusCode},错误原因{response.ReasonPhrase}"); } } public async Task DeleteAsync(string url, Dictionary dicHeaders, int timeoutSecond) { var client = _httpClientFactory.CreateClient(); var request = new HttpRequestMessage(HttpMethod.Delete, url); if (dicHeaders != null) { foreach (var head in dicHeaders) { request.Headers.Add(head.Key, head.Value); } } client.Timeout = TimeSpan.FromSeconds(timeoutSecond); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return result; } else { throw new CustomerHttpException($"接口请求错误,错误代码{response.StatusCode},错误原因{response.ReasonPhrase}"); } } /// /// 异步请求(通用) /// /// /// /// /// /// /// public async Task ExecuteAsync(string url, HttpMethod method, string requestString, Dictionary dicHeaders, int timeoutSecond = 120, string accept = "application/json") { var client = _httpClientFactory.CreateClient(); if (url.IndexOf('?') > -1) { url += "&v=" + DateTime.Now.ToString("yyyyMMddhhmmss"); } else { url += "?v=" + DateTime.Now.ToString("yyyyMMddhhmmss"); } var request = new HttpRequestMessage(method, url) { Content = new StringContent(requestString), }; request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); if (dicHeaders != null) { foreach (var header in dicHeaders) { request.Headers.Add(header.Key, header.Value); } } client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(accept) { CharSet = "utf-8" }); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var result = await response.Content.ReadAsStringAsync(); return result; } else { throw new CustomerHttpException($"接口请求错误,错误代码{response.StatusCode},错误原因{response.ReasonPhrase}"); } } } public class CustomerHttpException : Exception { public CustomerHttpException() : base() { } public CustomerHttpException(string message) : base(message) { } } }