首页 > 其他分享 >http请求的工具类

http请求的工具类

时间:2024-06-22 11:33:30浏览次数:11  
标签:http 请求 headers header client new 工具 null string

  1 public class HttpHelper
  2 {
  3     /// <summary>
  4     /// 发起POST同步请求
  5     /// </summary>
  6     /// <param name="url"></param>
  7     /// <param name="postData"></param>
  8     /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  9     /// <param name="headers">填充消息头</param>        
 10     /// <returns></returns>
 11     public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
 12     {
 13         postData ??= "";
 14         using HttpClient client = new HttpClient();
 15         client.Timeout = new TimeSpan(0, 0, timeOut);
 16         if (headers != null)
 17         {
 18             foreach (var header in headers)
 19                 client.DefaultRequestHeaders.Add(header.Key, header.Value);
 20         }
 21         using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
 22         if (contentType != null)
 23             httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
 24 
 25         HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
 26         return response.Content.ReadAsStringAsync().Result;
 27     }
 28 
 29     /// <summary>
 30     /// 发起POST异步请求
 31     /// </summary>
 32     /// <param name="url"></param>
 33     /// <param name="postData"></param>
 34     /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
 35     /// <param name="headers">填充消息头</param>        
 36     /// <returns></returns>
 37     public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
 38     {
 39         postData ??= "";
 40         using HttpClient client = new HttpClient();
 41         client.Timeout = new TimeSpan(0, 0, timeOut);
 42         if (headers != null)
 43         {
 44             foreach (var header in headers)
 45                 client.DefaultRequestHeaders.Add(header.Key, header.Value);
 46         }
 47         using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
 48         if (contentType != null)
 49             httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
 50 
 51         HttpResponseMessage response = await client.PostAsync(url, httpContent);
 52         return await response.Content.ReadAsStringAsync();
 53     }
 54 
 55     /// <summary>
 56     /// 发起GET同步请求
 57     /// </summary>
 58     /// <param name="url"></param>
 59     /// <param name="headers"></param>
 60     /// <param name="contentType"></param>
 61     /// <returns></returns>
 62     public static string HttpGet(string url, Dictionary<string, string> headers = null)
 63     {
 64         using HttpClient client = new HttpClient();
 65         if (headers != null)
 66         {
 67             foreach (var header in headers)
 68                 client.DefaultRequestHeaders.Add(header.Key, header.Value);
 69         }
 70         else
 71         {
 72             client.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");
 73             client.DefaultRequestHeaders.Add("UserAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
 74         }
 75         try
 76         {
 77             HttpResponseMessage response = client.GetAsync(url).Result;
 78             return response.Content.ReadAsStringAsync().Result;
 79         }
 80         catch (Exception ex)
 81         {
 82             //TODO 打印日志
 83             Console.WriteLine($"[Http请求出错]{url}|{ex.Message}");
 84         }
 85         return "";
 86     }
 87 
 88     /// <summary>
 89     /// 发起GET异步请求
 90     /// </summary>
 91     /// <param name="url"></param>
 92     /// <param name="headers"></param>
 93     /// <returns></returns>
 94     public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
 95     {
 96         using HttpClient client = new HttpClient();
 97         if (headers != null)
 98         {
 99             foreach (var header in headers)
100                 client.DefaultRequestHeaders.Add(header.Key, header.Value);
101         }
102         HttpResponseMessage response = await client.GetAsync(url);
103         return await response.Content.ReadAsStringAsync();
104     }
105 
106     /// <summary>
107     /// 发起Put同步请求
108     /// </summary>
109     /// <param name="url"></param>
110     /// <param name="postData"></param>
111     /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
112     /// <param name="headers">填充消息头</param>        
113     /// <returns></returns>
114     public static string HttpPut(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
115     {
116         postData ??= "";
117         using HttpClient client = new HttpClient();
118         client.Timeout = new TimeSpan(0, 0, timeOut);
119         if (headers != null)
120         {
121             foreach (var header in headers)
122                 client.DefaultRequestHeaders.Add(header.Key, header.Value);
123         }
124         using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
125         if (contentType != null)
126             httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
127 
128         HttpResponseMessage response = client.PutAsync(url, httpContent).Result;
129         return response.Content.ReadAsStringAsync().Result;
130     }
131 }

 

标签:http,请求,headers,header,client,new,工具,null,string
From: https://www.cnblogs.com/leon1128/p/18262031

相关文章

  • 如何使用SQL工具批量执行SQL文件?(以MySQL和SQLynx为例)
    目录1.配置MySQL数据源2.打开SQL文件3.执行SQL文件4.检查执行结果5.SQL文件示例6.注意事项7.总结在现代数据库管理和操作中,批量执行SQL文件在MySQL中显现出其巨大的价值和不可替代的作用。通过将多个SQL语句集成在一个文件中进行批量处理,数据库管理......
  • 日期工具类
    1publicclassDateTimeHelper2{3///<summary>4///5///</summary>6///<paramname="dateTime"></param>7///<returns></returns>8publicstaticDateTimeGetBeg......
  • Json工具类
    publicstaticclassJsonHelper{///<summary>///将对象序列化为JSON格式///</summary>///<paramname="o">对象</param>///<returns>json字符串</returns>publicstaticstringSerializeObject(object......
  • 无人直播不封号美女舞团3.0 多重防非操作(教程+素材+工具)
    在这个数字化的时代,直播已经成为了一种新的商业模式。然而,如何打造一个24小时自动循环播放的直播间,并通过此获得收益,却是许多人面临的挑战。本文将介绍如何通过使用OBS和咩播软件,创建一个能够吸引人们进入直播间并产生互动的直播环境,从而实现每天1000+,2000+的收益,甚至达到1......
  • 记一次https通讯调试过程
    情况说明:和服务端https交互时,用域名的方式会有正常的应答,用指定IP的方式则提示异常。代码抛出异常如下:javax.net.ssl.SSLHandshakeException:Remotehostclosedconnectionduringhandshakeatcom.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl,java:88......
  • STM32三种调试工具CMSIS-DAP、J-Link和ST-Link
    一.概述CMSIS-DAP、J-Link和ST-Link均是嵌入式处理器的开发调试工具。CMSIS-DAP是一种轻量级调试接口,旨在实现开源的开发调试。它的优点是使用方便、通用性好、成本低,还支持固件的在线升级。J-Link是一款由德国公司SEGGERMicrocontroller开发的高性能调试工具。但是价格较......
  • Camtasia 中视频制作和编辑的能力,让你能够创建更具创意和专业性的视频内容。随着实践
    Camtasia的初级应用,以下是一个简单的大纲,帮助你了解如何开始使用这款视频编辑和屏幕录制软件:1. 软件介绍和安装介绍Camtasia的功能和用途。下载和安装Camtasia软件。注册和激活软件(如果需要)。2. 录制屏幕启动Camtasia并了解用户界面。设置录制参数(如屏幕区域、......
  • 外链工具:助力高效外链建设
    免费外链工具:助力高效外链建设随着互联网的快速发展,SEO优化越来越受到网站管理者的重视。在SEO优化中,外链建设是非常重要的环节之一。外链建设能够有效提高网站的权重和排名,从而带来更多的流量和收益。然而,外链建设并不是一件容易的事情。需要寻找相关的网站进行交换链接,或者花......
  • 1v1直播源码,保证请求时序的两种常用方法
    1v1直播源码,保证请求时序的两种常用方法 在1v1直播源码中经常遇到请求输入查找场景,防抖与截流很好处理了频繁输入问题,但是不能解决最先发起请求结果后返回,覆盖了最后一次的搜索结果,导致搜索结果不正确。我总结一下自己常用的两种方法。一、使用时间戳来过滤返回结果如果请......
  • Fiddler 是一个功能强大的网络调试工具,通过掌握其高级功能,您可以更深入地进行流量分析
    Fiddler是一个功能强大的网络调试工具,主要用于捕获、检查和修改HTTP请求和响应。以下是一个Fiddler初级应用的大纲,帮助你快速了解如何使用它进行网络调试和分析:1. 安装和基本设置下载和安装Fiddler。启动Fiddler,并了解主界面的基本布局。配置浏览器或应用程序以使用......