Tuesday, 7 May 2013

C# .NET Web Client Example (GET & POST)

Making a simple web client in C# is pretty easy and straight forward. Every web client uses either GET or POST request to send data to the web server.


GET requests hold data variables to be sent right there in the URL like:
http://www.example.com/index.aspx?variable1=somedata&variable2=otherdata
 
The POST variable on the other hand set these variable inside the HTTP Header like this:
POST /index.aspx
Content-Type: application/x-www-form-urlencoded
Content-Length: 38
variable1=somedata&variable2=otherdata

 
Using the System.Net.WebRequest class we can get the above functionality

First the GET request code:
string proxyString = "XXX.XXX.XXX.XXX"; // Some proxy
string URL = "
http://www.example.com/index.aspx?variable1=somedata&variable2=otherdata";
System.Net.WebRequest req = System.Net.WebRequest.Create(URL);
req.Proxy = new System.Net.WebProxy(ProxyString, true);
System.Net.WebResponse resp = req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader( resp.GetResponseStream() );
System.Console.WriteLine( sr.ReadToEnd().Trim() );

 
Here,
  1. We define a proxy string that will be used later in the code. However, the “XXX” string is not valid here, just for example and the proxy is not used here as well.
  2. A URL is defined for GETting.
  3. A WebRequest object is created with the URL we defined.
  4. The true parameter tells the method that ‘no proxy is going to used here”.
  5. The GetResponse method uses the WebRequest object to access the URL and GET it and assign to a WebResponse object.
  6. We use the StreamReader to extract the data stream returned from the the request.
  7. The extracted data from the stream is printed on the screen.
Now, let us see the POST request code:

1:  string URL = "http://www.example.com/index.aspx";
2:  string proxyString = "XXX.XXX.XXX.XXX"; // Not used
3:  System.Net.WebRequest req = System.Net.WebRequest.Create(URL);
4:  req.Proxy = new System.Net.WebProxy(ProxyString, true);
5:  req.ContentType = "application/x-www-form-urlencoded";
6:  req.Method = "POST";
7:  string params = "variable1=somedata&variable2=otherdata";
8:  byte [] bytes = System.Text.Encoding.ASCII.GetBytes(params);
9:  req.ContentLength = bytes.Length;
10: System.IO.Stream stream = req.GetRequestStream();
11: stream.Write (bytes, 0, bytes.Length);
12: stream.Close();
13: System.Net.WebResponse resp = req.GetResponse();
14: if (resp != null) {
15:   System.IO.StreamReader sr = new System.IO.StreamReader( resp.GetResponseStream() );
16:   System.Console.WriteLine( sr.ReadToEnd().Trim() );
17: } else {
18:   System.Console.WriteLine("Invalid Response Received :(");
19: }


Here many lines are same as in the GET request code. The ones that are unique I will elaborate here,
  1. We don’t add the GET parameters after the main address here because this is a POST request and variables will go inside the HTTP Header instead.
  2. Same
  3. Same
  4. Same
  5. We need to add this Header field to tell the web server this is a form we are sending and it should parse it that way. Basically we are fooling the web server to think we are a web browser.
  6. We mention that it is a POST request we are sending.
  7. We mention the POST variables to be sent with the header.
  8. We assign the parameter string to bytes. Check next point.
  9. We assign the size of params byte variable we just used to the POST request we are to send.
  10. Now we obtain a stream from the request.
  11. Write the entire data we have to the stream that takes it to the target web server.
  12. Close the stream that we are done with it.
  13. Get response from target server.
  14. Check if the response is not null.
  15. Same
  16. Same
  17. If the response is null then-
  18. Print an error message.
This kind of code can be incorporated into a program to make simple to advanced web clients like web crawlers, browsers and network tools.

0 comments:

Post a Comment

All Articles © Asit. No Copying. Powered by Blogger.
 
Copyright © . The Tech Veda - Posts · Comments
Markup Powered by Bluefish Editor 2.2