zoukankan      html  css  js  c++  java
  • ASP.NET Core使用HttpClient的同步和异步请求

    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Common
    {
        /// <summary>
        /// 注意:HttpCient存在缺陷,高并发情况下会存在大量处于TIME_WAIT状态的套接字资源
        /// 因此可能造成套接字资源被耗尽,将抛出SocketException 错误。
        /// 高并发项目建议使用HttpWebRequest实现
        /// </summary>
        public class HttpHelper
        {
            /// <summary>
            /// 发起POST同步请求
            /// </summary>
            /// <param name="url">请求地址</param>
            /// <param name="body">POST提交的内容</param>
            /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded等</param>
            /// <param name="headers">请求头</param>
            /// <param name="timeOut">超时时间</param>
            /// <returns></returns>
            public static string HttpPost(string url, string body = null, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 30)
            {
                body = body ?? "";
                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(body, 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="body">POST提交的内容</param>
            /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
            /// <param name="headers">填充消息头</param>
            /// <param name="timeOut">超时时间</param>
            /// <returns></returns>
            public static async Task<string> HttpPostAsync(string url, string body = null, string contentType = null, Dictionary<string, string> headers = null, int timeOut = 30)
            {
                body = body ?? "";
                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(body, 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="timeOut">超时时间</param>
            /// <returns></returns>
            public static string HttpGet(string url, Dictionary<string, string> headers = null, int timeOut = 30)
            {
                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);
                    }
                    HttpResponseMessage response = client.GetAsync(url).Result;
                    return response.Content.ReadAsStringAsync().Result;
                }
            }
    
            /// <summary>
            /// 发起GET异步请求
            /// </summary>
            /// <param name="url">请求地址</param>
            /// <param name="headers">请求头</param>
            /// <param name="timeOut">超时时间</param>
            /// <returns></returns>
            public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null, int timeOut = 30)
            {
                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);
                    }
                    HttpResponseMessage response = await client.GetAsync(url);
                    return await response.Content.ReadAsStringAsync();
                }
            }
        }
    }

    调用方法:

    var headers = new Dictionary<string, string>();
    headers.Add("authorization", "eyJhbGciOiJodHRwOi8vd3d3LnczLXXXXXXX……");
    var result = await HttpHelper.HttpPostAsync(url, "{"name"="铁柱"}", "application/json", headers, 30);
  • 相关阅读:
    Android应用集成支付宝接口的简化
    【已解决】 c8812在eclipse上调试打不出log来?求帮助如何解决?!!!
    listview SimpleAdapter 例子
    倒计时器, android版 8秒 Thread Handle
    ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
    对比mako模板继承与Django的模板继承
    python自省反射【转】
    from gensee的视频web框架/ 豆瓣
    django区别对待127.0.0.1和localhost?
    Jquery学习
  • 原文地址:https://www.cnblogs.com/pudefu/p/7581956.html
Copyright © 2011-2022 走看看