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