Introduction
HttpWebRequest
/Response
- you read much of it, yet many tutorials, explanations, etc. are quite difficult. I'll try to explain the basics of HttpWebRequest
/Response
here, setting you off towards more advanced code with HTTP-usage.
This article is mostly intended to be read by beginning C# coders. Intermediate users might be able to use this as a small reference to some functions though. I'll try to do things structured.
The Code
Now, up to the code. In HttpWebRequest
/Response
, we have several functions, properties. These are the ones that will be explained here:
<p>WebRequest.Create(string RequestUri)<br />WebRequest.Create(System.Uri RequestUri)</p> | Used to initialize the HttpRequest variable |
String HttpWebRequest.Method | Used to set the method ("GET " or "POST ") of retrieving the data (Method header for HTTP/1.1) |
String HttpWebRequest.ContentType | Used for defining the content type (ContentType header for HTTP/1.1) |
HttpWebRequest.GetRequestStream() | Used to get the stream for putting POST information. |
HttpWebRequest.GetResponse() | Used to initialize the HttpWebResponse , and to get the data (HTML content) off the web |
HttpStatusCode HttpWebResponse.StatusCode | The statuscode returned by HttpWebResponse |
String HttpWebResponse.Server | The server (typically IIS or APACHE) returned by HttpWebResponse |
Now, up to some code.
First of all, we need or write namespaces. Here we will use:
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;
It's in the System.Net
the HttpWeb
- functions are found in.
We'll need the System.IO
for the stream reading/writing, and the System.Text
for the ASCII encoding.
Now, in your class, create a function (here it is start-post()
). This function is (for now) void
, and has no parameters, for we want to keep things simple in the beginning. If you find this too easy, you can always jump to parameters and return values immediately.
The code will be fairly simple, we'll try to extract some content from a server (in this case, a dummy script, the code for which I'll give you too).
private static void start_post()
{
byte[] buffer = Encoding.ASCII.GetBytes( "test=postvar&test2=another" );
HttpWebRequest WebReq =
(HttpWebRequest)WebRequest.Create("http://127.0.0.1/test.php");
WebReq.Method = "POST";
WebReq.ContentType ="application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
}
Now, this was simple, wasn't it?
Next, we'll do something even more simple... We'll GET a file on the web. We'll use quite the same functions, quite the same flow in the program, but with the difference that we don't need to put any requests (postvars
) into any form of stream. We'll have to use streams to get our output though.
Now we've got another function, start_get()
, again void
.
private static void start_get()
{
string getVars = "?var1=test1&var2=test2";
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create
(string.Format("http://127.0.0.1/test.php{0}", getVars));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
}
Again, the function is very easy...
Now, since we are all demanding coders, we won't quit with these simple functions, for we will have to change the functions, recompile the code, in order to change the file we need to get. We can use command-line input though. Why don't we upgrade our code? :)
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Text;
namespace HTTP
{
class HTTP_basics
{
private static void start_post(string strPage, string strBuffer)
{
byte[] buffer = Encoding.ASCII.GetBytes(strBuffer);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(strPage);
WebReq.Method = "POST";
WebReq.ContentType ="application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
}
private static void start_get(string strPage, string strVars)
{
HttpWebRequest WebReq =
(HttpWebRequest)WebRequest.Create(string.Format("{0}{1}", strPage, strVars));
WebReq.Method = "GET";
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
Console.WriteLine(WebResp.StatusCode);
Console.WriteLine(WebResp.Server);
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
}
public static void Main(string[] args)
{
while (true)
{
Console.WriteLine("What type? 1:Get, 2:POST");
int i = Int32.Parse(Console.ReadLine());
Console.WriteLine("What page?");
string page = Console.ReadLine();
Console.WriteLine("Vars? (ENTER for none)");
string vars = Console.ReadLine();
switch (i)
{
case 1: start_get(page, vars); break;
case 2: start_post(page, vars); break;
}
}
}
}
}
So, we have a small, text-based, none-doing browser from our console. It's very rough, but it was easy, and we learned a lot!
Now, I'll include the test.php code here (not difficult either):
<?php
print_r($_GET);
print_r($_POST);
?>
History
- 16-03-2007 0:40 Ending of first creation, any points you'd like to see changed/improved/added, please tell me! Hope this was interesting.