get/post 接口调用
生活随笔
收集整理的這篇文章主要介紹了
get/post 接口调用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
content-type:? application/~~~~~
/// <summary> /// Post數(shù)據(jù)到網(wǎng)站 /// </summary> /// <param name="posturl">網(wǎng)址</param> /// <param name="postData">參數(shù)</param> /// <returns></returns> public static string postHTTP(string posturl, string postData){Stream outstream = null;Stream instream = null;StreamReader sr = null;HttpWebResponse response = null;HttpWebRequest request = null;Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");byte[] data = encoding.GetBytes(postData);// 準(zhǔn)備請(qǐng)求... try{// 設(shè)置參數(shù) request = WebRequest.Create(posturl) as HttpWebRequest;CookieContainer cookieContainer = new CookieContainer();request.CookieContainer = cookieContainer;request.AllowAutoRedirect = true;request.Method = "POST";request.ContentType = "application/json";request.ContentLength = data.Length;outstream = request.GetRequestStream();outstream.Write(data, 0, data.Length);outstream.Close();//發(fā)送請(qǐng)求并獲取相應(yīng)回應(yīng)數(shù)據(jù) response = request.GetResponse() as HttpWebResponse;//直到request.GetResponse()程序才開始向目標(biāo)網(wǎng)頁(yè)發(fā)送Post請(qǐng)求 instream = response.GetResponseStream();sr = new StreamReader(instream, encoding);//返回結(jié)果網(wǎng)頁(yè)(html)代碼 string content = sr.ReadToEnd();string err = string.Empty;return content;}catch (Exception ex){string err = ex.Message;return string.Empty;}}?
IT用語(yǔ):通常這種同操作系統(tǒng)或其他應(yīng)用程序進(jìn)行交互的應(yīng)用程序請(qǐng)求稱為應(yīng)用程序接口,也就是常常提起的API
?
當(dāng)content-type:? application/json時(shí)
string tokenJson = "";Dictionary<string, string> par = new Dictionary<string, string>();par.Add("account", name);par.Add("secret", pwd);tokenJson = Tool.Json(par);tokenJson = Tool.Remove(tokenJson);string tokenResult = Tool.postHTTP("http://61.128.195.29:8180/bskyAPI/api/GP/GetToken", tokenJson);JObject tokenData = JObject.Parse(tokenResult);string token = tokenData["data"]["token"].ToString();return token;?
當(dāng)content-type:? application/x-www-from-urlencode時(shí)
參數(shù)格式為:name="zzzz"&id="aaaaa"
?
轉(zhuǎn)載于:https://www.cnblogs.com/yyl001/p/9047348.html
總結(jié)
以上是生活随笔為你收集整理的get/post 接口调用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android RecyclerView
- 下一篇: python3.6.0安装步骤