zoukankan      html  css  js  c++  java
  • HttpClient 一般Http请求的简单封装

      public class HttpHelper
        {/// <summary>
         /// 发起POST同步请求
         /// 
         /// </summary>
         /// <param name="url"></param>
         /// <param name="postData"></param>
         /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
         /// <param name="headers">填充消息头</param>        
         /// <returns></returns>
            public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
            {
                postData = postData ?? "";
                using (HttpClient client = new HttpClient())
                {
                    if (headers != null)
                    {
                        foreach (var header in headers)
                            client.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                    using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
                    {
                        if (contentType != null)
                            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
    
                        HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
                        return response.Content.ReadAsStringAsync().Result;
                    }
                }
            }
    
    
            /// <summary>
            /// 发起POST异步请求
            /// </summary>
            /// <param name="url"></param>
            /// <param name="postData"></param>
            /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
            /// <param name="headers">填充消息头</param>        
            /// <returns></returns>
            public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
            {
                postData = postData ?? "";
                using (HttpClient client = new HttpClient())
                {
                    client.Timeout = new TimeSpan(0, 0, timeOut);
                    if (headers != null)
                    {
                        foreach (var header in headers)
                            client.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                    using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
                    {
                        if (contentType != null)
                            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
    
                        HttpResponseMessage response = await client.PostAsync(url, httpContent);
                        return await response.Content.ReadAsStringAsync();
                    }
                }
            }
    
    
            /// <summary>
            /// 发起POST异步请求
            /// </summary>
            /// <param name="url"></param>
            /// <param name="httpContent"></param>
            /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
            /// <param name="headers">填充消息头</param>        
            /// <returns></returns>
            public static async Task<string> HttpPostAsync(string url, HttpContent httpContent, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
            {
    
                using (HttpClient client = new HttpClient())
                {
                    client.Timeout = new TimeSpan(0, 0, timeOut);
                    if (headers != null)
                    {
                        foreach (var header in headers)
                            client.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                    if (contentType != null)
                        httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
    
                    HttpResponseMessage response = await client.PostAsync(url, httpContent);
                    return await response.Content.ReadAsStringAsync();
                }
            }
    
            /// <summary>
            /// 发起GET同步请求
            /// </summary>
            /// <param name="url"></param>
            /// <param name="headers"></param>
            /// <param name="contentType"></param>
            /// <returns></returns>
            public static string HttpGet(string url, Dictionary<string, string> headers = null)
            {
                using (HttpClient client = new HttpClient())
                {
                    if (headers != null)
                    {
                        foreach (var header in headers)
                            client.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                    HttpResponseMessage response = client.GetAsync(url).Result;
                    return response.Content.ReadAsStringAsync().Result;
                }
            }
    
            /// <summary>
            /// 发起GET异步请求
            /// </summary>
            /// <param name="url"></param>
            /// <param name="headers"></param>
            /// <param name="contentType"></param>
            /// <returns></returns>
            public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
            {
                using (HttpClient client = new HttpClient())
                {
                    if (headers != null)
                    {
                        foreach (var header in headers)
                            client.DefaultRequestHeaders.Add(header.Key, header.Value);
                    }
                    HttpResponseMessage response = await client.GetAsync(url);
                    return await response.Content.ReadAsStringAsync();
                }
            }
        }
  • 相关阅读:
    进程和线程的区别?什么时候用进程?什么时候用线程?----看到好的复制到自己的园子里哈哈
    HTTPS详细讲解一篇就够了
    MySQL存储过程
    Spring注入全局的HttpServletRequest
    Java进阶必备
    Java8新特性
    java.time包常用类API学习记录
    Maven常用插件
    maven-dependency-versions-check-plugin, Maven 插件查找依赖版本冲突
    Jackson自定义注解
  • 原文地址:https://www.cnblogs.com/chongyao/p/12213068.html
Copyright © 2011-2022 走看看