IHttpClientFactory璋冪敤鎺ュ彛

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IHttpClientFactory璋冪敤鎺ュ彛相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/actor' title='actor'>actor   result   pass   ada   璇锋眰   tty   odi   get璇锋眰   efault   

 public class Monitor
    {       
        /// <summary>
        /// POST璇锋眰
        /// </summary>
        /// <param name="url"></param>
        /// <param name="obj"></param>
        /// <param name="contentType">application/xml銆乤pplication/json銆乤pplication/text銆乤pplication/x-www-form-urlencoded</param>
        /// <param name="charset"></param>
        /// <returns></returns>        
        public string HttpPostAsync(string url, object obj, string contentType = "", string charset = "UTF-8")
        {
            string result = "";

            var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
            IHttpClientFactory _httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
            var _httpClient = _httpClientFactory.CreateClient("CTCCMonitor");

            _httpClient.DefaultRequestHeaders.Accept.Clear();
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));

           
            string content = JsonConvert.SerializeObject(obj);

            var httpContent = new StringContent(content, Encoding.UTF8, contentType);

            var response = _httpClient.PostAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    result = t.Result;
                }
            }
            return result;
        }

        /// <summary>
        /// GET璇锋眰
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string HttpGetAsync(string url)
        {
            string result = "";
            var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
            IHttpClientFactory _httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();

            var _httpClient = _httpClientFactory.CreateClient("CTCCMonitor");

            var response = _httpClient.GetAsync(url).Result;

            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    result = t.Result;
                }
            }
            return result;
        }
    }

string url="";//鎺ュ彛鍦板潃

//Post璇锋眰

 string postData = "method=" + method + "&user_id=" + user_id + "&passWord=" + passwordEnc + "&sign=" + sign;
apiResult = monitor.HttpPostAsync(url, postData, "application/x-www-form-urlencoded");

 

string requestUrl = url + "?method=" + method + "&user_id=" + user_id + "&passWord=" + passwordEnc + "&sign=" + sign;
apiResult = monitor.HttpGetAsync(requestUrl);

銆€銆€

以上是关于IHttpClientFactory璋冪敤鎺ュ彛的主要内容,如果未能解决你的问题,请参考以下文章

寰俊api璋冪敤闄愬埗锛?5009 reach max api daily quota limit 瑙e喅鏂规硶

UDP socket涔熷彲浠ヤ娇鐢╟onnect绯荤粺璋冪敤

vue鍏ュ彛鏂囦欢

璁捐妯″紡

OC璋冪敤Swift

Dubbo鎺ュ彛涓嶇煡閬撴€庝箞娴嬶紵鏁欎綘涓夎浠g爜瀹炵幇dubbo鎺ュ彛娴嬭瘯