A rarely updated blog about programming and lack of sleep

How to use HttpWebRequest in C#

    Kragerø

    The HttpWebRequest in C# can be really useful. With it, you can get the latest weather in to your application, or your choice of news, or any info you want from the internet. Or post info to a server on the web. One useful idea is to make a game which use HttpWebRequest to post its score to your webserver. I can show you how to do the serverside later, this time i'm going to show you how to use HttpWebRequest.

    The HTTP protocol

    To understand how to use HttpWebRequest, you will have to understand how a client (your webbrowser) ask a server (the webpage) for information. You will have to understand the HTTP protocol.After you written in the url in your address bar and pressed enter, and after your computer have translated it to a ip-adress (read more about dns somewhere else), your browser will connect to the server hosting the webpage you are looking for. Then your browser will send this text to the server:

    GET /index.html HTTP/1.1
    Host: www.google.com
    [more optional headers]
    [empty line]

    In reply, the server will send back this:

    HTTP/1.1 200 OK
    [lot of other boring headers]
    [blank line]
    [The content, aka. pure html (usually)]

    This is stuff you can even do manually yourself, without bothering with boring cpu draining browsers. Open up your command line utility by searching your Start-menu for cmd.exe (i'm using Windows, if you are not, your on yourself, buddy). Write "telnet www.onlyhuman.dk 80" in the command line and press enter... Your greeted by a completely black nothing and your cursor... Dont touch anything yet, because anything written will be sent to the webserver. And when you start writing, ignore the fact that nothing is shown.. Windows Telnet is... weird sometimes.

    While ignoring the fact that you can't see what you write, write really carefull "GET / HTTP/1.1" and press "enter" twice. If you did it right, there is going to appear a lot of text. Scroll up and check the first line. It will be "HTTP/1.1 200 OK" and that means that we understood you, we found your page and all is OK. Then some boring info and last you get to see the webpage. Dont mind the lack of pictures... Or the fact that the text you got was not my page at all!

    Explanations! HTTP/1.1 is the protocol and its version, so that both you and the server know what to tell each other. "200 OK" is a status code. Look them up to find out what they all mean. The most famous status code is the "404 Page not found". The first slash / is the path. You could write /index.php to get my index page. You will only get a  Or you could write /blog7/ to get my blog frontpage. But you would only get a 404 status code. Thats because we lack one line which is getting increasingly important (and became required in the HTTP version 1.1), and thats the "Host" line.

    Start telnet again (telnet www.onlyhuman.dk 80). Write:

    GET /index.php HTTP/1.1
    HOST: www.onlyhuman.dk
    [press enter twice]

    Holy crap, another status code? Its just me having forwarded my frontpage.. Look it up. But why did you have to write Host: www.onlyhuman.dk? Because the ip address which my blog is on, is also shared by many, many other webpages. To separate them, the server needs to know where you want to go. It's much like when you send a letter; if a housenumber (the ip-address is a house number) has multiple residents, you also have to write the name of the recipient.

    And you did GET that GET means "i want to get that page", right? You get it, eh? Get?! Haha... not really funny, eh? ForGET it.
    But instead of GET, you can also write other stuff. Like HEAD. This will make the server respond only with its header text, so you not have 200 pages of HTML to scroll trough. Its really nice if you just want to get some information about the server. Or you can write POST. This is used for POSTing information. Like when you going to POST your awesome scores.With POST, in addition to the headers, you are able to add a body to your webrequest. This can be XML, or a list of values, or whatever. It's all up to what the server want you to send it. And even if you are using POST, you still get a response from the server, which can be XML, a list of values, or a webpage.

    And theres even more methods, google them.

    How to use HttpWebRequest in C#

    To use HttpWebRequest in C#, create a HttpWebRequest object.

    string url = "http://www.onlyhuman.dk/";
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

    To set which method you wish to use (POST or GET), do this:

    req.Method = "GET";

    If you are using method GET, this is all you need and you can start reading the response. We'll do that in a minute. If you are using the POST method, you will also have to add a body to our request. We have to add this using streams.. Why does everything microsoft have to have a stream? I love strings, they are much easier to manage... Well, until a certain point. Read it up somewhere. To add your data to the request, you will have to know what kind of data the server expects. Usually it is form data, like when you use HTML to create a form and a submit button. Other times its XML, like when you send a blog post to your blog. Lets assume its a form for now:

    req.ContentType = "application/x-www-form-urlencoded";
    string postData = "score=9001&initials=TAV";
    using(StreamWriter stream = new StreamWriter(req.GetRequestStream()))
    { stream.Write(postData); }

    Thats it, now the postData, or the request body, have been added to our request..

    Reading the response

    Wether we are using GET or POST, the response is a stream. So we have to use a StreamReader to read it and turn it into a string (unless you prefer to work in streams, of course *cough*madman*). Lets create a string to hold our result first:

    string result = "";

    Before we read the string, we have to make sure to know if something goes wrong. Then we can read the string.

    try {
    using (StreamReader stream = new StreamReader(req.GetResponse().GetResponseStream()))
    { result = stream.ReadToEnd(); }
    }

    To catch whatever went wrong (theres always something) and output it to the string instead of our expected results:

    catch (WebException ex)
    { result = ex.Status.ToString();
    if(ex.Response.ContentLength != 0)
    {using (var stream = ex.Response.GetResponseStream())
    {using (var reader = new StreamReader(stream))
    { result += reader.ReadToEnd(); }
    }}}

    Now the string "Result" will contain your result, wether its a failure or not, wether its a webpage, pure text or an xml file.

    Add new comment