<tr id="ohcdf"></tr>
    <ins id="ohcdf"><option id="ohcdf"></option></ins>

    <ins id="ohcdf"></ins>
  1. <tr id="ohcdf"></tr>

      <small id="ohcdf"></small><tr id="ohcdf"><small id="ohcdf"><delect id="ohcdf"></delect></small></tr><code id="ohcdf"></code>
        潤宇軟件
        首 頁 企業簡介 項目案例 軟件定制 行業軟件 解決方案 企業資訊 服務專區 客服中心
        業務介紹:西安軟件公司、軟件開發、軟件定制、軟件外包
        軟件 方案 文章
          潤宇軟件 >> 新聞資訊  >> 解決方案

        西安軟件公司講解HttpWebResponse 調用WebApi

        發布時間:2019/2/25  瀏覽次數:17次  字體【    】
        軟件公司講解開發C#.net調用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;
                            }
                        }
                    }
                }
            }
          關閉本頁
        西部IT網合作伙伴 合作伙伴
        陜西省 | 榆林 | 延安 | 銅川 | 渭南 | 商洛 | 寶雞 | 漢中 | 安康 | 咸陽
        網站首頁 | 關于我們 | 售后服務 | 項目合同 | 查看留言 | 在線留言 | 客服中心
        © 版權所有:西安潤宇軟件科技有限公司 
        公司地址:西安市絲路國際創意夢工廠4號樓 聯系電話:029-87878512 手機:13468700578 聯系人:李先生
        Copyright ® 2009-2015 RunYusoft.com Inc. All Rights Reserved 
        技術支持:西安潤宇軟件科技有限公司  陜ICP備11000720號
        精品久久一区二区三区无码,久久亚洲AV成人无码国产精品,久久久久精品a毛片,亚洲精品无码九九九九