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);
  • 相关阅读:
    XML 编码
    XML CDATA
    XML 命名空间
    XML 解析器
    XML XMLHttpRequest 对象
    XML 和CSS
    XML 验证
    XML 属性
    XML 元素
    XML 语法规则
  • 原文地址:https://www.cnblogs.com/pudefu/p/7581956.html
Copyright © 2011-2022 走看看