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");

 

posted @ 2017-11-23 15:55  炮跑灰  阅读(9421)  评论(0编辑  收藏  举报