HttpClientHelper

    public class HttpClientHelper
    {
        private const int TimeOutValue = 60000;

        public string GetAsync(string url)
        {
            HttpClientHandler handler = new HttpClientHandler
            {
                AutomaticDecompression = DecompressionMethods.GZip
            };
            HttpClient client = new HttpClient(handler);
            client.Timeout = TimeSpan.FromMilliseconds(60000.0);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            return client.GetStringAsync(url).Result;
        }

        public string GetAsyncUri(string url)
        {
            HttpClient client = new HttpClient();
            client.Timeout = TimeSpan.FromMilliseconds(60000.0);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage result = client.GetAsync(url).Result;
            return result.RequestMessage.RequestUri.AbsoluteUri;
        }
    }

 

posted @ 2021-08-20 11:30  simadi  阅读(81)  评论(0编辑  收藏  举报