日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

API请求接口-远程服务器返回错误: (400) 错误的请求错误

發(fā)布時間:2023/12/31 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 API请求接口-远程服务器返回错误: (400) 错误的请求错误 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

捕獲異常查看具體錯誤

using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.IO; using System.Net; using System.Reflection; using System.Text;namespace Test {class Program{static void Main(string[] args){string url = "http://www.baidus.com";string contentType = "application/x-www-form-urlencoded";int timeOut = 3000;string parms = "626";int encoding = 65001;HttpWebRequest(url, parms, encoding, contentType, timeOut);}public static void HttpWebRequest(string url, string parms, int encoding, string contentType = "application/x-www-form-urlencoded", int timeOut = 3000){try{string result = string.Empty;var mRequest = (HttpWebRequest)WebRequest.Create(url);//相應(yīng)請求的參數(shù)var data = Encoding.GetEncoding(encoding).GetBytes(parms);mRequest.Method = "Post";mRequest.ContentType = contentType;mRequest.ContentLength = data.Length;mRequest.Timeout = timeOut;mRequest.KeepAlive = true;mRequest.ProtocolVersion = HttpVersion.Version10;//請求流var requestStream = mRequest.GetRequestStream();requestStream.Write(data, 0, data.Length);requestStream.Close();//響應(yīng)流var mResponse = mRequest.GetResponse() as HttpWebResponse;//此位置進入catch(403)var responseStream = mResponse.GetResponseStream();//if (responseStream != null){var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));//獲取返回的信息result = streamReader.ReadToEnd();streamReader.Close();responseStream.Close();}}catch (System.Net.WebException ex){ //進入異常 在異常里獲取 返回的數(shù)據(jù),有時候在調(diào)試的時候 直接異常進入這里,我們得不到數(shù)據(jù),//如果用Fiddler我們查看訪問的結(jié)果是有數(shù)據(jù)的,經(jīng)過下面的處理,最終得到數(shù)據(jù)string result = string.Empty;//響應(yīng)流var mResponse = ex.Response as HttpWebResponse;var responseStream = mResponse.GetResponseStream();if (responseStream != null){var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));//獲取返回的信息result = streamReader.ReadToEnd();streamReader.Close();responseStream.Close();}result = "獲取數(shù)據(jù)失敗,請重試!" + url + ex.ToString() + " 返回數(shù)據(jù)" + result;}}}}

總結(jié)

以上是生活随笔為你收集整理的API请求接口-远程服务器返回错误: (400) 错误的请求错误的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。