C# HttpClient帮助类

    public class HttpClientHelp
    {
        // Post请求
        public static T PostResponse(string url, string postData, int iWaitTime = 3000)
            where T : class, new()
        {
            try
            {
                if (url.StartsWith("https"))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }

                HttpContent httpContent = new StringContent(postData);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                HttpClient httpClient = new HttpClient();

                HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
                if (response != null && response.IsSuccessStatusCode)
                {
                    Task t = response.Content.ReadAsStringAsync();
                    if (!t.Wait(iWaitTime))
                    {
                        return null;
                    }
                    return JsonConvert.DeserializeObject(t.Result);
                }

                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static bool PostResponse(string url, string postData)
        {
            try
            {
                if (url.StartsWith("https"))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }

                HttpContent httpContent = new StringContent(postData);
                httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                //httpContent.Headers.ContentType.CharSet = "UTF-8";
                HttpClient httpClient = new HttpClient();

                httpClient.PostAsync(url, httpContent);

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        // Get请求
        public static T GetResponse(string url, int iWaitTime = 3000)
           //where T : class, new()
        {
            try
            {
                if (url.StartsWith("https"))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }

                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = httpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead).Result;
                if (response != null && response.IsSuccessStatusCode)
                {
                    Task t = response.Content.ReadAsStringAsync();
                    if (!t.Wait(iWaitTime))
                    {
                        return default(T);
                    }

                    return JsonConvert.DeserializeObject(t.Result);
                }

                return default(T); ;
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }

        public static bool GetResponse(string url)
        {
            try
            {
                if (url.StartsWith("https"))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }

                var httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

 

 

你可能感兴趣的:(C#,Web,http)