zoukankan      html  css  js  c++  java
  • c# HttpWebResponse 调用WebApi

    public static class WebApiCaller
        {
            public static string HttpPost(string url, string body)
            {
                try
                {
                    //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    Encoding encoding = Encoding.UTF8;
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    request.Method = "POST";
                    request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
                    request.ContentType = "application/json; charset=utf-8";
    
                    byte[] buffer = encoding.GetBytes(body);
                    request.ContentLength = buffer.Length;
                    request.GetRequestStream().Write(buffer, 0, buffer.Length);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                    {
                        return reader.ReadToEnd();
                    }
                }
                catch (WebException ex)
                {
                    var res = (HttpWebResponse)ex.Response;
                    StringBuilder sb = new StringBuilder();
                    StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
                    sb.Append(sr.ReadToEnd());
                    //string ssb = sb.ToString();
                    throw new Exception(sb.ToString());
                }
            }
    
            /// <summary>  
            /// GET Method  
            /// </summary>  
            /// <returns></returns>  
            public static string HttpGet(string url)
            {
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "GET";
    
                HttpWebResponse myResponse = null;
                try
                {
                    myResponse = (HttpWebResponse)myRequest.GetResponse();
                    StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    string content = reader.ReadToEnd();
                    return content;
                }
                //异常请求  
                catch (WebException e)
                {
                    myResponse = (HttpWebResponse)e.Response;
                    using (Stream errData = myResponse.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(errData))
                        {
                            string text = reader.ReadToEnd();
    
                            return text;
                        }
                    }
                }
            }
        }
    string result = WebApiCaller.HttpPost("http://localhost:8082/api/Patient/SavePatient", jsonString);
    string result1 = WebApiCaller.HttpGet("http://localhost:8080/api/Patient/GetPatientInfoById?Id=55");
  • 相关阅读:
    Leetcode(337)-打家劫舍III
    Leetcode(213)-打家劫舍II
    Leetcode(198)-打家劫舍
    Leetcode(32)-最长有效括号
    计数排序
    智能指针
    C++中的explicit
    Leetcode(878)-第 N 个神奇数字
    Leetcode(877)-石子游戏
    C++的memset
  • 原文地址:https://www.cnblogs.com/qq458978/p/8920796.html
Copyright © 2011-2022 走看看