在WinForm中發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)方法
更新時(shí)間:2016年06月07日 10:51:00 投稿:jingxian
下面小編就為大家?guī)?lái)一篇在WinForm中發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
如何在WinForm中請(qǐng)求發(fā)送HTTP
手工發(fā)送HTTP請(qǐng)求主要是調(diào)用 System.Net的HttpWebResponse方法
手工發(fā)送HTTP的GET請(qǐng)求:
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch?keyword="; strURL +=this.textBox1.Text; System.Net.HttpWebRequest request; // 創(chuàng)建一個(gè)HTTP請(qǐng)求 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); //request.Method="get"; System.Net.HttpWebResponse response; response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.Stream s; s = response.GetResponseStream(); XmlTextReader Reader = new XmlTextReader(s); Reader.MoveToContent(); string strValue = Reader.ReadInnerXml(); strValue = strValue.Replace("<","<"); strValue = strValue.Replace(">",">"); MessageBox.Show(strValue); Reader.Close();
手工發(fā)送HTTP的POST請(qǐng)求
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch"; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); //Post請(qǐng)求方式 request.Method="POST"; // 內(nèi)容類型 request.ContentType="application/x-www-form-urlencoded"; // 參數(shù)經(jīng)過(guò)URL編碼 string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword"); paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox1.Text); byte[] payload; //將URL編碼后的字符串轉(zhuǎn)化為字節(jié) payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); //設(shè)置請(qǐng)求的 ContentLength request.ContentLength = payload.Length; //獲得請(qǐng) 求流 Stream writer = request.GetRequestStream(); //將請(qǐng)求參數(shù)寫入流 writer.Write(payload,0,payload.Length); // 關(guān)閉請(qǐng)求流 writer.Close(); System.Net.HttpWebResponse response; // 獲得響應(yīng)流 response = (System.Net.HttpWebResponse)request.GetResponse(); System.IO.Stream s; s = response.GetResponseStream(); XmlTextReader Reader = new XmlTextReader(s); Reader.MoveToContent(); string strValue = Reader.ReadInnerXml(); strValue = strValue.Replace("<","<"); strValue = strValue.Replace(">",">"); MessageBox.Show(strValue); Reader.Close();
以上這篇在WinForm中發(fā)送HTTP請(qǐng)求的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Unity實(shí)現(xiàn)車型識(shí)別的示例代碼
這篇文章主要介紹了在Unity中接入百度AI,實(shí)現(xiàn)檢測(cè)一張車輛圖片的具體車型。即對(duì)于輸入的一張圖片(可正常解碼,且長(zhǎng)寬比適宜),輸出圖片的車輛品牌及型號(hào)。需要的可以參考一下2022-01-01C# MJPEG 客戶端簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了C# MJPEG 客戶端簡(jiǎn)單實(shí)現(xiàn)的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03