C#调用http请求,HttpWebRequest添加http请求头信息

最近经常涉及到与各大web接口对接,调用对方webapi 的业务需要。无奈写下此小工具,方便后期摘抄,也希望能帮助到需要的你。废话不多说,直接上干货:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Net;
  6 using System.Text;
  7 using System.Threading.Tasks;
  8 
  9 namespace http
 10 {
 11     class Program
 12     {
 13         static void Main(string[] args)
 14         {            
 15             string Datas = @"{ ""cainfo"": ""1"", ""dev_no"": ""1"", ""dev_safe_info"": ""1"", ""fixmedins_code"": ""060070"", ""fixmedins_name"": ""朝阳医院"", ""inf_time"": ""2020-09-29 02:49:11"", ""infno"": ""1101"", ""infver"": ""V1.0"", ""insuplc_admdvs"": ""340600"", ""mdtrtarea_admvs"": ""340600"", ""msgid"": ""1232342423"", ""opter"": ""14"", ""opter_name"": ""csb-测试"", ""opter_type"": ""1"", ""recer_sys_code"": ""1"", ""sign_no"": ""1"", ""signtype"": ""1"" }";
 16             //调用post请求 
 17             string retString = httpPost("http://127.0.0.1/admin/ajax/test", Datas);
 18            
 19             //调用get请求
 20             string retString2 = httpGet("http://127.0.0.1/admin/ajax/test");            
 21             //http://csb-broker.cloud.ahhb.hsip.gov.cn:8086/1.0.0/hsa-fsi-1101
 22 
 23             Console.WriteLine(retString);
 24             Console.WriteLine("--===========↑上面是post请求返回的数据 ↓下面是get请求返回的数据===============");
 25             Console.WriteLine(retString2);
 26             Console.ReadKey();
 27         }
 28 
 29 
 30         public static string httpPost(string url,string data) {
 31            
 32             string retString = String.Empty;
 33             string paramData = data;
 34             //CookieContainer cookie = new CookieContainer();
 35             //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://csb-broker.cloud.ahhb.hsip.gov.cn:8086/1.0.0/hsa-fsi-1101");
 36             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 37             request.Method = "POST";
 38             //request.Referer = Referer;
 39             byte[] bytes = Encoding.UTF8.GetBytes(paramData);
 40             request.Accept = "text/html, application/xhtml+xml, */*";
 41             //request.ContentType = "application/*";
 42             request.ContentType = "application/x-www-form-urlencoded";
 43             //设置C# HttpWebRequest post请求头消息
 44             request.Headers.Add("version", "1.0.0");
 45             request.Headers.Add("ak", "45b83b3b780d46028afa1da41dcdd0a4");
 46             request.Headers.Add("sk", "1+JX1Gsg3hEu4Ui+VdQvWlaz1gY=");
 47             request.ContentLength = bytes.Length;
 48             try
 49             {
 50                 Stream myResponseStream = request.GetRequestStream();
 51                 myResponseStream.Write(bytes, 0, bytes.Length);
 52                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 53                 StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
 54                 retString = myStreamReader.ReadToEnd();
 55                 int statusCode = (int)response.StatusCode;
 56                 if (statusCode == 200)
 57                 {
 58                     // Console.WriteLine(statusCode);
 59                     myStreamReader.Close();
 60                     myResponseStream.Close();
 61 
 62                     if (response != null)
 63                     {
 64                         response.Close();
 65                     }
 66                     if (request != null)
 67                     {
 68                         request.Abort();
 69                     }
 70                 }
 71             }
 72             catch (Exception ex)
 73             {
 74                 //抛出异常返回具体错误消息
 75                 retString =  ex.Message;
 76             }
 77             return retString;          
 78         }
 79 
 80 
 81         public static string httpGet(string Url)
 82         {
 83             string retString = string.Empty;
 84             //System.GC.Collect();
 85             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
 86             request.Proxy = null;
 87             request.KeepAlive = false;
 88             request.Method = "GET";
 89             request.ContentType = "application/json; charset=UTF-8";
 90             request.AutomaticDecompression = DecompressionMethods.GZip;
 91             try
 92             {
 93                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 94                 Stream myResponseStream = response.GetResponseStream();
 95                 StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
 96                 retString = myStreamReader.ReadToEnd();
 97                 myStreamReader.Close();
 98                 myResponseStream.Close();
 99 
100                 if (response != null)
101                 {
102                     response.Close();
103                 }
104                 if (request != null)
105                 {
106                     request.Abort();
107                 }
108             }catch(Exception ex){
109                 //抛出异常返回具体错误消息
110                 retString = ex.Message;
111             }
112             return retString;
113         }
114     }
115 }


 

运行的效果如下:

 

把post和get放在一起调用了 都调用成功的情况

C#调用http请求,HttpWebRequest添加http请求头信息

 

 

主机不存在的情况或者是服务端没有打开的情况

 

C#调用http请求,HttpWebRequest添加http请求头信息

 

 

服务端错误情况

 

C#调用http请求,HttpWebRequest添加http请求头信息

 

上一篇:API-6_4 C#请求Web API


下一篇:C#模拟登录网页---HttpWebRequest