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,
- 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.
- A URL is defined for GETting.
- A WebRequest object is created with the URL we defined.
- The true parameter tells the method that ‘no proxy is going to used here”.
- The GetResponse method uses the WebRequest object to access the URL and GET it and assign to a WebResponse object.
- We use the StreamReader to extract the data stream returned from the the request.
- The extracted data from the stream is printed on the screen.
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,
- 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.
- Same
- Same
- Same
- 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.
- We mention that it is a POST request we are sending.
- We mention the POST variables to be sent with the header.
- We assign the parameter string to bytes. Check next point.
- We assign the size of params byte variable we just used to the POST request we are to send.
- Now we obtain a stream from the request.
- Write the entire data we have to the stream that takes it to the target web server.
- Close the stream that we are done with it.
- Get response from target server.
- Check if the response is not null.
- Same
- Same
- If the response is null then-
- Print an error message.
0 comments:
Post a Comment