From 74f3a2357cf0095c3f977770d38a235c028ce68e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Sun, 28 May 2023 13:14:54 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20HttpHelper=20=E6=96=B0=E5=A2=9Eput?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/Helper/HttpHelper.cs | 142 +++++++++++++++------------- 1 file changed, 77 insertions(+), 65 deletions(-) diff --git a/Infrastructure/Helper/HttpHelper.cs b/Infrastructure/Helper/HttpHelper.cs index e7193f8..75ac10d 100644 --- a/Infrastructure/Helper/HttpHelper.cs +++ b/Infrastructure/Helper/HttpHelper.cs @@ -10,7 +10,6 @@ namespace Infrastructure { /// /// 发起POST同步请求 - /// /// /// /// @@ -19,26 +18,21 @@ namespace Infrastructure /// public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary headers = null) { - postData = postData ?? ""; - using (HttpClient client = new HttpClient()) - { - client.Timeout = new TimeSpan(0, 0, timeOut); - if (headers != null) - { - foreach (var header in headers) - client.DefaultRequestHeaders.Add(header.Key, header.Value); - } - using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8)) - { - if (contentType != null) - httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); - - HttpResponseMessage response = client.PostAsync(url, httpContent).Result; - return response.Content.ReadAsStringAsync().Result; - } + postData ??= ""; + using HttpClient client = new HttpClient(); + client.Timeout = new TimeSpan(0, 0, timeOut); + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); } - } + using HttpContent httpContent = new StringContent(postData, Encoding.UTF8); + if (contentType != null) + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); + HttpResponseMessage response = client.PostAsync(url, httpContent).Result; + return response.Content.ReadAsStringAsync().Result; + } /// /// 发起POST异步请求 @@ -50,24 +44,20 @@ namespace Infrastructure /// public static async Task HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary headers = null) { - postData = postData ?? ""; - using (HttpClient client = new HttpClient()) + postData ??= ""; + using HttpClient client = new HttpClient(); + client.Timeout = new TimeSpan(0, 0, timeOut); + if (headers != null) { - client.Timeout = new TimeSpan(0, 0, timeOut); - if (headers != null) - { - foreach (var header in headers) - client.DefaultRequestHeaders.Add(header.Key, header.Value); - } - using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8)) - { - if (contentType != null) - httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); - - HttpResponseMessage response = await client.PostAsync(url, httpContent); - return await response.Content.ReadAsStringAsync(); - } + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); } + using HttpContent httpContent = new StringContent(postData, Encoding.UTF8); + if (contentType != null) + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); + + HttpResponseMessage response = await client.PostAsync(url, httpContent); + return await response.Content.ReadAsStringAsync(); } /// @@ -79,30 +69,28 @@ namespace Infrastructure /// public static string HttpGet(string url, Dictionary headers = null) { - using (HttpClient client = new HttpClient()) + using HttpClient client = new HttpClient(); + if (headers != null) { - if (headers != null) - { - foreach (var header in headers) - client.DefaultRequestHeaders.Add(header.Key, header.Value); - } - else - { - client.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded"); - client.DefaultRequestHeaders.Add("UserAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); - } - try - { - HttpResponseMessage response = client.GetAsync(url).Result; - return response.Content.ReadAsStringAsync().Result; - } - catch (Exception ex) - { - //TODO 打印日志 - Console.WriteLine($"[Http请求出错]{url}|{ex.Message}"); - } - return ""; + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); } + else + { + client.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded"); + client.DefaultRequestHeaders.Add("UserAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); + } + try + { + HttpResponseMessage response = client.GetAsync(url).Result; + return response.Content.ReadAsStringAsync().Result; + } + catch (Exception ex) + { + //TODO 打印日志 + Console.WriteLine($"[Http请求出错]{url}|{ex.Message}"); + } + return ""; } /// @@ -113,16 +101,40 @@ namespace Infrastructure /// public static async Task HttpGetAsync(string url, Dictionary headers = null) { - using (HttpClient client = new HttpClient()) + using HttpClient client = new HttpClient(); + if (headers != null) { - if (headers != null) - { - foreach (var header in headers) - client.DefaultRequestHeaders.Add(header.Key, header.Value); - } - HttpResponseMessage response = await client.GetAsync(url); - return await response.Content.ReadAsStringAsync(); + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); } + HttpResponseMessage response = await client.GetAsync(url); + return await response.Content.ReadAsStringAsync(); + } + + /// + /// 发起Put同步请求 + /// + /// + /// + /// application/xml、application/json、application/text、application/x-www-form-urlencoded + /// 填充消息头 + /// + public static string HttpPut(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary headers = null) + { + postData ??= ""; + using HttpClient client = new HttpClient(); + client.Timeout = new TimeSpan(0, 0, timeOut); + if (headers != null) + { + foreach (var header in headers) + client.DefaultRequestHeaders.Add(header.Key, header.Value); + } + using HttpContent httpContent = new StringContent(postData, Encoding.UTF8); + if (contentType != null) + httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); + + HttpResponseMessage response = client.PutAsync(url, httpContent).Result; + return response.Content.ReadAsStringAsync().Result; } } }