94 lines
3.0 KiB
C#
94 lines
3.0 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Net.Http;
|
|||
|
|
using System.Net.Http.Headers;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Hncore.Infrastructure.Serializer;
|
|||
|
|
|
|||
|
|
namespace Hncore.Infrastructure.Extension
|
|||
|
|
{
|
|||
|
|
public static class HttpClientFactoryExtension
|
|||
|
|
{
|
|||
|
|
public static HttpClient CreateClient(this IHttpClientFactory factory, TimeSpan timeOut)
|
|||
|
|
{
|
|||
|
|
var client = factory.CreateClient();
|
|||
|
|
|
|||
|
|
client.Timeout = timeOut;
|
|||
|
|
|
|||
|
|
return client;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static class HttpClientExtension
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// post请求,ContentType:application/json
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="httpClient"></param>
|
|||
|
|
/// <param name="path"></param>
|
|||
|
|
/// <param name="data"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static async Task<HttpResponseMessage> PostAsJson(this HttpClient httpClient, string path, object data,
|
|||
|
|
Encoding encoding = null)
|
|||
|
|
{
|
|||
|
|
if (encoding == null)
|
|||
|
|
{
|
|||
|
|
encoding = Encoding.UTF8;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string content = "";
|
|||
|
|
|
|||
|
|
if (data is string s)
|
|||
|
|
{
|
|||
|
|
content = s;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
content = data.ToJson();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
HttpContent httpContent = new StringContent(content, encoding);
|
|||
|
|
|
|||
|
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|||
|
|
|
|||
|
|
return await httpClient.PostAsync(path, httpContent);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// post请求,ContentType:application/json
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="httpClient"></param>
|
|||
|
|
/// <param name="path"></param>
|
|||
|
|
/// <param name="data"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static async Task<string> PostAsJsonGetString(this HttpClient httpClient, string path, object data,
|
|||
|
|
Encoding encoding = null)
|
|||
|
|
{
|
|||
|
|
var res = await httpClient.PostAsJson(path, data, encoding);
|
|||
|
|
|
|||
|
|
return await res.Content.ReadAsStringAsync();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static async Task<HttpResponseMessage> PostAsForm(this HttpClient httpClient, string path, IEnumerable<KeyValuePair<string, string>> data,
|
|||
|
|
Encoding encoding = null)
|
|||
|
|
{
|
|||
|
|
if (encoding == null)
|
|||
|
|
{
|
|||
|
|
encoding = Encoding.UTF8;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
HttpContent httpContent = new FormUrlEncodedContent(data);
|
|||
|
|
|
|||
|
|
return await httpClient.PostAsync(path, httpContent);
|
|||
|
|
}
|
|||
|
|
public static async Task<string> PostAsFormGetString(this HttpClient httpClient, string path, IEnumerable<KeyValuePair<string, string>> data,
|
|||
|
|
Encoding encoding = null)
|
|||
|
|
{
|
|||
|
|
var resp=await httpClient.PostAsForm(path, data, encoding);
|
|||
|
|
|
|||
|
|
return await resp.Content.ReadAsStringAsync();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|