|
|
|
|
namespace HttpClientStudy.UnitTest.HttpClients
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 简单 HttpClient 包装类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class BaseHttpClient
|
|
|
|
|
{
|
|
|
|
|
#region Get请求
|
|
|
|
|
public string Get(string url)
|
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
//可以统一设置HttpClient属性:HttpClient可以使用统一设置进行多次请求。
|
|
|
|
|
//client.BaseAddress = new Uri(url);
|
|
|
|
|
//client.DefaultRequestHeaders.Add("Accept", "application/json");
|
|
|
|
|
//client.DefaultRequestVersion = HttpVersion.Version10;
|
|
|
|
|
//client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrHigher;
|
|
|
|
|
//client.Timeout = TimeSpan.FromSeconds(60);
|
|
|
|
|
//client.CancelPendingRequests();
|
|
|
|
|
|
|
|
|
|
//只有Send是同步方法,其它全为异步方法。
|
|
|
|
|
|
|
|
|
|
//Send同步方法,需要HttpRequestMessage参数
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
|
|
|
|
var response = client.Send(request);
|
|
|
|
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
//或者
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
//业务逻辑
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//状态码
|
|
|
|
|
if (response is { StatusCode: HttpStatusCode.OK })
|
|
|
|
|
{
|
|
|
|
|
//业务逻辑
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//请求头
|
|
|
|
|
if (response is { Headers.ETag: null })
|
|
|
|
|
{
|
|
|
|
|
//业务逻辑
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = response.Content.ReadAsStringAsync().Result;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> GetAsync(string url)
|
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
var response = await client.GetAsync(url);
|
|
|
|
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TResult? GetJson<TResult>(string url)
|
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
|
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
|
|
|
|
var response = client.Send(request);
|
|
|
|
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
if (response.Content.Headers.ContentType?.MediaType != MediaTypeNames.Application.Json)
|
|
|
|
|
{
|
|
|
|
|
throw new HttpRequestException("响应不是 json 格式!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = response.Content.ReadFromJsonAsync<TResult>().Result;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<TResult?> GetJsonAsync<TResult>(string url)
|
|
|
|
|
{
|
|
|
|
|
var client = new HttpClient();
|
|
|
|
|
|
|
|
|
|
var response = await client.GetAsync(url);
|
|
|
|
|
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
if (response.Content.Headers.ContentType?.MediaType != MediaTypeNames.Application.Json)
|
|
|
|
|
{
|
|
|
|
|
throw new HttpRequestException("响应不是 json 格式!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await response.Content.ReadFromJsonAsync<TResult>();
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|