49 lines
1.7 KiB
C#
49 lines
1.7 KiB
C#
|
|
using Amazon.Runtime;
|
|||
|
|
using langguanApi.Extensions.AutoDI;
|
|||
|
|
|
|||
|
|
namespace langguanApi.Common.Proxy
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// http
|
|||
|
|
/// </summary>
|
|||
|
|
[ServiceInjection(InjectionType.Singleton)]
|
|||
|
|
public class HttpProxy
|
|||
|
|
{
|
|||
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|||
|
|
public HttpProxy(IHttpClientFactory httpClientFactory)
|
|||
|
|
{
|
|||
|
|
_httpClientFactory = httpClientFactory;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"></typeparam>
|
|||
|
|
/// <param name="parameters"></param>
|
|||
|
|
/// <param name="requestUri"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
/// <exception cref="Exception"></exception>
|
|||
|
|
public async Task<T> Get<T>(Dictionary<string, string> parameters, string requestUri)
|
|||
|
|
{
|
|||
|
|
//从工厂获取请求对象
|
|||
|
|
var client = _httpClientFactory.CreateClient();
|
|||
|
|
if (parameters != null)
|
|||
|
|
{
|
|||
|
|
var strParam = string.Join("&", parameters.Select(o => o.Key + "=" + o.Value));
|
|||
|
|
requestUri = string.Concat(requestUri, '?', strParam);
|
|||
|
|
}
|
|||
|
|
client.BaseAddress = new Uri(requestUri);
|
|||
|
|
var resp = await client.GetAsync(requestUri);
|
|||
|
|
if (resp.IsSuccessStatusCode)
|
|||
|
|
{
|
|||
|
|
var content = await resp.Content.ReadAsStringAsync();
|
|||
|
|
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(content);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"请求失败,状态码:{resp.StatusCode},请求地址 {requestUri}");
|
|||
|
|
return default(T);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|