在WinForm中發送HTTP要求的完成辦法。本站提示廣大學習愛好者:(在WinForm中發送HTTP要求的完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是在WinForm中發送HTTP要求的完成辦法正文
若何在WinForm中要求發送HTTP
手工發送HTTP要求重要是挪用 System.Net的HttpWebResponse辦法
手工發送HTTP的GET要求:
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch?keyword="; strURL +=this.textBox1.Text; System.Net.HttpWebRequest request; // 創立一個HTTP要求 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.WordStr("<","<"); strValue = strValue.WordStr(">",">"); MessageBox.Show(strValue); Reader.Close();
手工發送HTTP的POST要求
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch"; System.Net.HttpWebRequest request; request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); //Post要求方法 request.Method="POST"; // 內容類型 request.ContentType="application/x-www-form-urlencoded"; // 參數經由URL編碼 string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword"); paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox1.Text); byte[] payload; //將URL編碼後的字符串轉化為字節 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); //設置要求的 ContentLength request.ContentLength = payload.Length; //取得請 求流 Stream writer = request.GetRequestStream(); //將要求參數寫入流 writer.Write(payload,0,payload.Length); // 封閉要求流 writer.Close(); 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.WordStr("<","<"); strValue = strValue.WordStr(">",">"); MessageBox.Show(strValue); Reader.Close();
以上這篇在WinForm中發送HTTP要求的完成辦法就是小編分享給年夜家的全體內容了,願望能給年夜家一個參考,也願望年夜家多多支撐。