How to send HTTP requests in WinForm?
How to send HTTP requests in WinForm
To send an HTTP request manually, call the HttpWebResponse method of System. Net.
To manually send an http get request:
String strURL = "http: // localhost/Play/movie/Service1.asmx/doSearch? Keyword = "; strURL + = this. textBox1.Text; System. net. httpWebRequest request; // create an 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 ("& lt;", "<"); strValue = strValue. replace ("& gt;", ">"); MessageBox. show (strValue); Reader. close ();/* Ask hovertree.com */
Manually send an http post request
String strURL =" http://localhost/Play/CH1/Service1.asmx/doSearch "; System. net. httpWebRequest request; request = (System. net. httpWebRequest) WebRequest. create (strURL); // Post request method. method = "POST"; // content type request. contentType = "application/x-www-form-urlencoded"; // The parameter is URL encoded string paraUrlCoded = System. web. httpUtility. urlEncode ("keyword"); paraUrlCoded + = "=" + System. web. httpUtility. urlEncode (this. textBox1.Text); byte [] payload; // convert the URL-encoded string into byte payload = System. text. encoding. UTF8.GetBytes (paraUrlCoded); // you can specify the ContentLength of the request. contentLength = payload. length; // obtain Stream writer = request. getRequestStream (); // write the request parameters to the stream writer. write (payload, 0, payload. length); // close the request stream writer. close (); System. net. httpWebResponse response; // get response stream 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 ("& lt;", "<"); strValue = strValue. replace ("& gt;", ">"); MessageBox. show (strValue); Reader. close ();/* Ask hovertree.com */