如何在WinForm中发送HTTP请求
生活随笔
收集整理的這篇文章主要介紹了
如何在WinForm中发送HTTP请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Winform窗體中發送HTTP請求
手工發送HTTP請求主要是調用 System.Net的HttpWebResponse方法
手工發送HTTP的GET請 求:
?
手工發送HTTP的POST請求
1 string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch"; 2 System.Net.HttpWebRequest request; 3 4 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); 5 //Post請求方式 6 request.Method="POST"; 7 // 內容類型 8 request.ContentType="application/x-www-form-urlencoded"; 9 // 參數經過URL編碼 10 string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword"); 11 paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox1.Text); 12 byte[] payload; 13 //將URL編碼后的字符串轉化為字節 14 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); 15 //設置請求的 ContentLength 16 request.ContentLength = payload.Length; 17 //獲得請 求流 18 Stream writer = request.GetRequestStream(); 19 //將請求參數寫入流 20 writer.Write(payload,0,payload.Length); 21 // 關閉請求流 22 writer.Close(); 23 System.Net.HttpWebResponse response; 24 // 獲得響應流 25 response = (System.Net.HttpWebResponse)request.GetResponse(); 26 System.IO.Stream s; 27 s = response.GetResponseStream(); 28 XmlTextReader Reader = new XmlTextReader(s); 29 Reader.MoveToContent(); 30 string strValue = Reader.ReadInnerXml(); 31 strValue = strValue.Replace("<","<"); 32 strValue = strValue.Replace(">",">"); 33 MessageBox.Show(strValue); 34 Reader.Close();?
?
轉載于:https://www.cnblogs.com/MDK-L/p/3791303.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的如何在WinForm中发送HTTP请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: day8:文件操作
- 下一篇: IPv6应用普及,任重而道远