在WinForm中發(fā)送HTTP請求的實現(xiàn)方法
如何在WinForm中請求發(fā)送HTTP
手工發(fā)送HTTP請求主要是調(diào)用 System.Net的HttpWebResponse方法
手工發(fā)送HTTP的GET請求:
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch?keyword="; strURL +=this.textBox1.Text; System.Net.HttpWebRequest request; // 創(chuàng)建一個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.Replace("<","<"); strValue = strValue.Replace(">",">"); MessageBox.Show(strValue); Reader.Close();
手工發(fā)送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"; // 內(nèi)容類型 request.ContentType="application/x-www-form-urlencoded"; // 參數(shù)經(jīng)過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è)置請求的 ContentLength request.ContentLength = payload.Length; //獲得請 求流 Stream writer = request.GetRequestStream(); //將請求參數(shù)寫入流 writer.Write(payload,0,payload.Length); // 關(guān)閉請求流 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請求的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持我們。
欄 目:C#教程
本文標題:在WinForm中發(fā)送HTTP請求的實現(xiàn)方法
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6476.html
您可能感興趣的文章
- 01-10C#通過反射獲取當前工程中所有窗體并打開的方法
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新聞效果的方法
- 01-10C#實現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10winform實現(xiàn)創(chuàng)建最前端窗體的方法
- 01-10C#實現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10WinForm實現(xiàn)自定義右下角提示效果的方法
- 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時間
- 01-10.net2.0+ Winform項目實現(xiàn)彈出容器層


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改