✨ HttpHelper 新增put同步请求
This commit is contained in:
parent
c76e698ed7
commit
74f3a2357c
@ -10,7 +10,6 @@ namespace Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// 发起POST同步请求
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="postData"></param>
|
||||
@ -19,26 +18,21 @@ namespace Infrastructure
|
||||
/// <returns></returns>
|
||||
public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发起POST异步请求
|
||||
@ -50,24 +44,20 @@ namespace Infrastructure
|
||||
/// <returns></returns>
|
||||
public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -79,30 +69,28 @@ namespace Infrastructure
|
||||
/// <returns></returns>
|
||||
public static string HttpGet(string url, Dictionary<string, string> 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 "";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -113,16 +101,40 @@ namespace Infrastructure
|
||||
/// <returns></returns>
|
||||
public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> 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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发起Put同步请求
|
||||
/// </summary>
|
||||
/// <param name="url"></param>
|
||||
/// <param name="postData"></param>
|
||||
/// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
|
||||
/// <param name="headers">填充消息头</param>
|
||||
/// <returns></returns>
|
||||
public static string HttpPut(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user