How to send HTTP requests in WinForm?

Source: Internet
Author: User

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 */

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.