81 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			2.9 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;
 | 
						|
        /// <summary>
 | 
						|
        /// 构造函数
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="httpClientFactory"></param>
 | 
						|
        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("httpreq");
 | 
						|
            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);
 | 
						|
            }
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// post数据
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="requestUri"></param>
 | 
						|
        /// <param name="data"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<T> PostData<T>(object data, string requestUri)
 | 
						|
        {
 | 
						|
            StringContent content = null;
 | 
						|
            var client = _httpClientFactory.CreateClient();
 | 
						|
            client.BaseAddress = new Uri(requestUri);
 | 
						|
            if (data != null)
 | 
						|
            {
 | 
						|
                string json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
 | 
						|
                content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
 | 
						|
            }
 | 
						|
            var response = await client.PostAsync(requestUri, content);
 | 
						|
            if (response.IsSuccessStatusCode)
 | 
						|
            {
 | 
						|
                var result = await response.Content.ReadAsStringAsync();
 | 
						|
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(result);
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                Console.WriteLine($"请求失败,状态码:{response.StatusCode},请求地址 {requestUri}");
 | 
						|
                return default(T);
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |