zoukankan      html  css  js  c++  java
  • HttpClient实现同步异步GET,POST

            /// <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 ??= "";
                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 = "application/json", int timeOut = 30, Dictionary<string, string> headers = null)
            {
                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>
            /// 发起GET同步请求
            /// </summary>
            /// <param name="url"></param>
            /// <param name="headers"></param>
            /// <param name="contentType"></param>
            /// <returns></returns>
            public static string HttpGet(string url, string contentType = null, Dictionary<string, string> headers = null)
            {
                using HttpClient client = new HttpClient();
                if (contentType != null)
                    client.DefaultRequestHeaders.Add("ContentType", contentType);
                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, string contentType = null, Dictionary<string, string> headers = null)
            {
                using HttpClient client = new HttpClient();
                if (contentType != null)
                    client.DefaultRequestHeaders.Add("ContentType", contentType);
                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();
            }
  • 相关阅读:
    selenium 下载百度音乐并验证
    selenium web driver 实现截图功能
    selenium web driver 使用JS修改input属性
    .net测试学习--理解.net测试选项
    试水STF(smartphone test farm)
    The difference between QA, QC, and Test Engineering
    selenium 3.0 beta2 初体验
    Gson解析纯Json数组
    Android带图片的Toast(自定义Toast)
    Android issues
  • 原文地址:https://www.cnblogs.com/Gold-fangjin/p/12172165.html
Copyright © 2011-2022 走看看