A relatively simple Web request (Get/post) method encapsulates the ability to invoke a network interface

Source: Internet
Author: User

Send Web requests, get requests, and post requests

<summary>

Send a GET request
</summary>
<param name= "url" ></param>
<returns></returns>
public static string Webrequest (string url)
{
WebRequest wr = webrequest.create (URL);

Url Credentials = CredentialCache.DefaultCredentials;
wr. Method = "GET";
wr. ContentType = "application/x-www-form-urlencoded";


WebResponse response = WR. GetResponse ();
Stream DataStream = Response. GetResponseStream ();


Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
Read the content.
String responsefromserver = reader. ReadToEnd ();
Reader. Close ();
Datastream.close ();
Response. Close ();
return responsefromserver;
}
<summary>
POST request
</summary>
<param name= "url" ></param>
<param name= "PostData" > Parameters after binary </param>
<returns></returns>
public static string Webrequest (string URL, byte[] postdata)
{
WebRequest wr = webrequest.create (URL);




Url Credentials = CredentialCache.DefaultCredentials;
Url Method = "POST";
Url ContentType = "application/x-www-form-urlencoded";
Url ContentLength = PostData. Length;
Stream SW = WR. GetRequestStream ();
Sw. Write (postdata, 0, PostData. Length);
Sw. Close ();


WebResponse response = WR. GetResponse ();
Stream DataStream = Response. GetResponseStream ();


Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (DataStream, Encoding.default);
Read the content.
String responsefromserver = reader. ReadToEnd ();
Reader. Close ();
Datastream.close ();
Response. Close ();
return responsefromserver;
}

A relatively simple Web request (Get/post) method encapsulates the ability to invoke a network interface

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.