Implementation method of sending HTTP request in WinForm
- 2021-10-13 08:25:30
- OfStack
How to Request to Send HTTP in WinForm
Manually sending HTTP requests is mainly to call HttpWebResponse methods of System. Net
Manually send GET requests for HTTP:
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch?keyword=";
strURL +=this.textBox1.Text;
System.Net.HttpWebRequest request;
// Create 1 A HTTP Request
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();
Manually send POST requests for HTTP
string strURL = "http://localhost/Play/CH1/Service1.asmx/doSearch";
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
//Post Request mode
request.Method="POST";
// Content type
request.ContentType="application/x-www-form-urlencoded";
// Parameter passing URL Code
string paraUrlCoded = System.Web.HttpUtility.UrlEncode("keyword");
paraUrlCoded += "=" + System.Web.HttpUtility.UrlEncode(this.textBox1.Text);
byte[] payload;
// Will URL The encoded string is converted to bytes
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
// Set the requested ContentLength
request.ContentLength = payload.Length;
// Get an invitation Flow seeking
Stream writer = request.GetRequestStream();
// Write request parameters to the stream
writer.Write(payload,0,payload.Length);
// Close the request flow
writer.Close();
System.Net.HttpWebResponse response;
// Get the response flow
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();