c# HttpWebResponse 调用WebApi

新建WebApiCaller类,内容如下:

 1 public static class WebApiCaller
 2     {
 3         public static string HttpPost(string url, string body)
 4         {
 5             try
 6             {
 7                 //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
 8                 Encoding encoding = Encoding.UTF8;
 9                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
10                 request.Method = "POST";
11                 request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
12                 request.ContentType = "application/json; charset=utf-8";
13 
14                 byte[] buffer = encoding.GetBytes(body);
15                 request.ContentLength = buffer.Length;
16                 request.GetRequestStream().Write(buffer, 0, buffer.Length);
17                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
18                 using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
19                 {
20                     return reader.ReadToEnd();
21                 }
22             }
23             catch (WebException ex)
24             {
25                 var res = (HttpWebResponse)ex.Response;
26                 StringBuilder sb = new StringBuilder();
27                 StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
28                 sb.Append(sr.ReadToEnd());
29                 //string ssb = sb.ToString();
30                 throw new Exception(sb.ToString());
31             }
32         }
33 
34         /// <summary>  
35         /// GET Method  
36         /// </summary>  
37         /// <returns></returns>  
38         public static string HttpGet(string url)
39         {
40             HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
41             myRequest.Method = "GET";
42 
43             HttpWebResponse myResponse = null;
44             try
45             {
46                 myResponse = (HttpWebResponse)myRequest.GetResponse();
47                 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
48                 string content = reader.ReadToEnd();
49                 return content;
50             }
51             //异常请求  
52             catch (WebException e)
53             {
54                 myResponse = (HttpWebResponse)e.Response;
55                 using (Stream errData = myResponse.GetResponseStream())
56                 {
57                     using (StreamReader reader = new StreamReader(errData))
58                     {
59                         string text = reader.ReadToEnd();
60 
61                         return text;
62                     }
63                 }
64             }
65         }
66     }

 

用法:

string result = WebApiCaller.HttpPost("http://localhost:8082/api/Patient/SavePatient", jsonString);
string result1 = WebApiCaller.HttpGet("http://localhost:8080/api/Patient/GetPatientInfoById?Id=55");

 

转载于:https://www.cnblogs.com/paopaohui/p/7885312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值