✨ HttpHelper 新增put同步请求
This commit is contained in:
parent
c76e698ed7
commit
74f3a2357c
@ -10,7 +10,6 @@ namespace Infrastructure
|
|||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发起POST同步请求
|
/// 发起POST同步请求
|
||||||
///
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="url"></param>
|
/// <param name="url"></param>
|
||||||
/// <param name="postData"></param>
|
/// <param name="postData"></param>
|
||||||
@ -19,26 +18,21 @@ namespace Infrastructure
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
|
public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
|
||||||
{
|
{
|
||||||
postData = postData ?? "";
|
postData ??= "";
|
||||||
using (HttpClient client = new HttpClient())
|
using HttpClient client = new HttpClient();
|
||||||
{
|
|
||||||
client.Timeout = new TimeSpan(0, 0, timeOut);
|
client.Timeout = new TimeSpan(0, 0, timeOut);
|
||||||
if (headers != null)
|
if (headers != null)
|
||||||
{
|
{
|
||||||
foreach (var header in headers)
|
foreach (var header in headers)
|
||||||
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
||||||
}
|
}
|
||||||
using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
|
using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
|
||||||
{
|
|
||||||
if (contentType != null)
|
if (contentType != null)
|
||||||
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
||||||
|
|
||||||
HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
|
HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
|
||||||
return response.Content.ReadAsStringAsync().Result;
|
return response.Content.ReadAsStringAsync().Result;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发起POST异步请求
|
/// 发起POST异步请求
|
||||||
@ -50,25 +44,21 @@ namespace Infrastructure
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
|
public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
|
||||||
{
|
{
|
||||||
postData = postData ?? "";
|
postData ??= "";
|
||||||
using (HttpClient client = new HttpClient())
|
using HttpClient client = new HttpClient();
|
||||||
{
|
|
||||||
client.Timeout = new TimeSpan(0, 0, timeOut);
|
client.Timeout = new TimeSpan(0, 0, timeOut);
|
||||||
if (headers != null)
|
if (headers != null)
|
||||||
{
|
{
|
||||||
foreach (var header in headers)
|
foreach (var header in headers)
|
||||||
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
client.DefaultRequestHeaders.Add(header.Key, header.Value);
|
||||||
}
|
}
|
||||||
using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
|
using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
|
||||||
{
|
|
||||||
if (contentType != null)
|
if (contentType != null)
|
||||||
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
|
||||||
|
|
||||||
HttpResponseMessage response = await client.PostAsync(url, httpContent);
|
HttpResponseMessage response = await client.PostAsync(url, httpContent);
|
||||||
return await response.Content.ReadAsStringAsync();
|
return await response.Content.ReadAsStringAsync();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发起GET同步请求
|
/// 发起GET同步请求
|
||||||
@ -79,8 +69,7 @@ namespace Infrastructure
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string HttpGet(string url, Dictionary<string, string> headers = null)
|
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)
|
foreach (var header in headers)
|
||||||
@ -103,7 +92,6 @@ namespace Infrastructure
|
|||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发起GET异步请求
|
/// 发起GET异步请求
|
||||||
@ -113,8 +101,7 @@ namespace Infrastructure
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
|
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)
|
foreach (var header in headers)
|
||||||
@ -123,6 +110,31 @@ namespace Infrastructure
|
|||||||
HttpResponseMessage response = await client.GetAsync(url);
|
HttpResponseMessage response = await client.GetAsync(url);
|
||||||
return await response.Content.ReadAsStringAsync();
|
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