The implementation method of sending HTTP requests in WinForm _c# tutorial

Source: Internet
Author: User
Tags http post urlencode

How to request to send HTTP in WinForm

Sending HTTP requests manually is primarily a HttpWebResponse method of calling System.Net

To send HTTP GET requests manually:

String strURL = "http://localhost/Play/CH1/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 ("<", "<");
strvalue = Strvalue.replace (">", ">");
MessageBox.Show (strvalue); 
Reader.close ();

Send an HTTP POST request manually

String strURL = "Http://localhost/Play/CH1/Service1.asmx/doSearch";

System.Net.HttpWebRequest request;
Request = (System.Net.HttpWebRequest) webrequest.create (strURL); Post request method request.
Method= "POST"; Content type request.
Contenttype= "application/x-www-form-urlencoded";
The parameter is encoded by a URL encoding string paraurlcoded = System.Web.HttpUtility.UrlEncode ("keyword");
paraurlcoded + = "=" + System.Web.HttpUtility.UrlEncode (this.textBox1.Text);
Byte[] payload;
Converts a URL-encoded string into a byte payload = System.Text.Encoding.UTF8.GetBytes (paraurlcoded); Sets the requested ContentLength request. ContentLength = payload.
Length; Get request Stream Stream writer = requests.
GetRequestStream (); Writes the request parameter to the stream writer. Write (payload,0,payload.
Length); Closes 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 ("<", "<");
strvalue = Strvalue.replace (">", ">"); 
MessageBox.Show (strvalue);
 Reader.close ();

Above this in the WinForm to send the HTTP request Realization method is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

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.