如何在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è)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注