NuGet下載Flurl FlurlHttpClient類 public class FlurlHttpClient { private readonly FlurlClient client; public FlurlHttpClient(FlurlClient client) { this.cl ...
NuGet下載Flurl
FlurlHttpClient類
public class FlurlHttpClient
{
private readonly FlurlClient client;
public FlurlHttpClient(FlurlClient client)
{
this.client = client;
}
public async Task<T> GetAsync<T>(string url) where T : class
{
try
{
return await client.Request(url).GetJsonAsync<T>();
}
catch (FlurlHttpException ex)
{
string method = ex.Call.HttpRequestMessage.Method.Method; // 獲取請求方式
IFlurlResponse? response = ex.Call.Response;
int? statusCode = response is null ? null : response.StatusCode;//伺服器狀態碼
string errorMsg = $"伺服器狀態碼:{statusCode};Url:{ex.Call.Request.Url};請求方式:{method},入參:無;message:{ex.Message}";
throw new Exception(errorMsg);
}
}
/// <summary>
/// Get請求
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url">url</param>
/// <param name="values">參數以對象的形式傳入</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<T> GetAsync<T>(string url, object values)
{
try
{
return await client.Request(url).SetQueryParams(values).GetJsonAsync<T>();
}
catch (FlurlHttpException ex)
{
string method = ex.Call.HttpRequestMessage.Method.Method; // 獲取請求方式
IFlurlResponse? response = ex.Call.Response;
string? statusCode = response is null ? null : response.StatusCode.ToString();//伺服器狀態碼
string errorMsg = $"伺服器狀態碼:{statusCode ?? "無"};Url:{ex.Call.Request.Url};請求方式:{method};入參:{values};message:{ex.Message}";
throw new Exception(errorMsg);
}
}
/// <summary>
/// Post請求
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="url">url</param>
/// <param name="values">參數以對象的形式傳入</param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<T> PostAsync<T>(string url,object values)
{
try
{
//.ReceiveJson<T>()方法來指定響應的JSON數據會被反序列化為類型T
return await client.Request(url).PostJsonAsync(values).ReceiveJson<T>();
}
catch (FlurlHttpException ex)
{
string method = ex.Call.HttpRequestMessage.Method.Method; // 獲取請求方式
IFlurlResponse? response = ex.Call.Response;
int? statusCode = response is null ? null : response.StatusCode;//伺服器狀態碼
string errorMsg = $"伺服器狀態碼:{statusCode};Url:{ex.Call.Request.Url};請求方式:{method};入參:{values};message:{ex.Message}";
throw new Exception(errorMsg);
}
}
}
註入FlurlHttpClient類
/// <summary>
/// 初始化flurlhttpclient
/// </summary>
/// <param name="builder"></param>
public static void InitFlurlHttpClient(this WebApplicationBuilder builder)
{
var baseUrl = builder.Configuration["wmsBaseUrl"];
if (string.IsNullOrEmpty(baseUrl)) throw new Exception("baseUrl未配置");
//WithHeader添加請求頭
builder.Services.AddScoped(provider => new FlurlClient(baseUrl).WithHeader("test","111"));
builder.Services.AddScoped(provider => {
var flurlClient = provider.GetService<FlurlClient>(); // 從依賴註入容器中獲取 FlurlClient 類型
if (flurlClient is null) throw new Exception("系統錯誤flurlClient類還未註入");
return new FlurlHttpClient(flurlClient);
});
}
使用
[HttpGet]
public async Task<ActionResult<ApiResponse>> Get()
{
var resAsync = httpClient.GetAsync<object>("TakeNumber/GetNumber", new { boxNumber = "123456" });
return this.Success(resAsync);
}
[HttpPost]
public async Task<ActionResult<ApiResponse>> Post()
{
var resAsync = httpClient.PostAsync<object>("TakeNumber/GetNumber", new { boxNumber = "123456" });
return this.Success(resAsync);
}