Click here to Skip to main content
6,292,426 members and growing! (10,140 online)
Email Password   helpLost your password?
Web Development » Web Services » General     Beginner

HttpWebRequest/Response in a nutshell - Part 1

By CPF_

Using HttpWebRequest/Response to do basic web surfing.
C# 2.0, Windows, .NET, Visual Studio, Dev
Posted:15 Mar 2007
Views:78,294
Bookmarked:74 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
17 votes for this article.
Popularity: 5.21 Rating: 4.23 out of 5
1 vote, 6.3%
1

2
1 vote, 6.3%
3
3 votes, 18.8%
4
11 votes, 68.8%
5

Introduction

HttpWebRequest/Response. You read much of it, yet many tutorials, explainations, 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:

WebRequest.Create(string RequestUri)
WebRequest.Create(System.Uri RequestUri)

Used to initialise the HttpRequest variable
String HttpWebRequest.MethodUsed to set the method ("GET" or "POST") of retreiving the data. (Method header for HTTP/1.1)
String HttpWebRequest.ContentTypeUsed 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 initialise the HttpWebResponse, and to get the data (html content) off the web.
HttpStatusCode HttpWebResponse.StatusCodeThe statuscode returned by HttpWebResponse.
String HttpWebResponse.ServerThe server (typically IIS or APACHE) returned by HttpWebResponse

Now, up to some code.
First of all, we need or right 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 immediatelly.
The code will be fairly simple, we'll try to extract some content from a server (in this case, a dummy script, which code I'll give you too)

private static void start_post()
{
    //Our postvars
    byte[]  buffer = Encoding.ASCII.GetBytes( "test=postvar&test2=another" );
    //Initialisation, we use localhost, change if appliable
    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/test.php");
    //Our method is post, otherwise the buffer (postvars) would be useless
    WebReq.Method = "POST";
    //We use form contentType, for the postvars.
    WebReq.ContentType ="application/x-www-form-urlencoded";
    //The length of the buffer (postvars) is used as contentlength.
    WebReq.ContentLength = buffer.Length;
    //We open a stream for writing the postvars
    Stream PostData = WebReq.GetRequestStream();
    //Now we write, and afterwards, we close. Closing is always important!
    PostData.Write(buffer, 0, buffer.Length);
    PostData.Close();
    //Get the response handle, we have no true response yet!
    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
    //Let's show some information about the response
    Console.WriteLine(WebResp.StatusCode);
    Console.WriteLine(WebResp.Server);
           
    //Now, we read the response (the string), and output it.
    Stream Answer = WebResp.GetResponseStream();
    StreamReader _Answer = new StreamReader(Answer);
    Console.WriteLine(_Answer.ReadToEnd());

    //Congratulations, you just requested your first POST page, you
    //can now start logging into most login forms, with your application
    //Or other examples.
} 

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()
{
    //Our getVars, to test the get of our php. We can get a page without any of these vars too though.
    string getVars = "?var1=test1&var2=test2";
    //Initialisation, we use localhost, change if appliable
    HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("http://127.0.0.1/test.php{0}", getVars));
    //This time, our method is GET.
    WebReq.Method = "GET";
    //From here on, it's all the same as above.
    HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
    //Let's show some information about the response
    Console.WriteLine(WebResp.StatusCode);
    Console.WriteLine(WebResp.Server);
           
    //Now, we read the response (the string), and output it.
           Stream Answer = WebResp.GetResponseStream();
           StreamReader _Answer = new StreamReader(Answer);
           Console.WriteLine(_Answer.ReadToEnd());
    
    //Congratulations, with these two functions in basic form, you just learned
    //the two basic forms of web surfing
    //This proves how easy it can be.
}  

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)
        {
            //Our postvars
            byte[]  buffer = Encoding.ASCII.GetBytes(strBuffer);
            //Initialisation
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(strPage);
            //Our method is post, otherwise the buffer (postvars) would be useless
            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType ="application/x-www-form-urlencoded";
            //The length of the buffer (postvars) is used as contentlength.
            WebReq.ContentLength = buffer.Length;
            //We open a stream for writing the postvars
            Stream PostData = WebReq.GetRequestStream();
            //Now we write, and afterwards, we close. Closing is always important!
            PostData.Write(buffer, 0, buffer.Length);
            PostData.Close();
            //Get the response handle, we have no true response yet!
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            //Let's show some information about the response
            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);
           
            //Now, we read the response (the string), and output it.
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            Console.WriteLine(_Answer.ReadToEnd());
        }   

        private static void start_get(string strPage, string strVars)
        {
            //Initialisation
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("{0}{1}", strPage, strVars));
            //This time, our method is GET.
            WebReq.Method = "GET";
            //From here on, it's all the same as above.
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            //Let's show some information about the response
            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);
           
            //Now, we read the response (the string), and output it.
            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);  //Prints all GET variables in a quite readable manner.
 print_r($_POST); //Prints all POST variables in a quite readable manner.
?> 

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

CPF_


Member
Started coding at age of 12 (approx.) started out with pascal, went to Delphi, and from Delphi to BC++ (all in one year), did several years with BC++, and later on started to learn some html, php, mysql... Now more into C#, python, php.
Current age: 17
Location: Belgium Belgium

Other popular Web Services articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
Generalhandling session Pinmemberaamirzada23:50 30 Jun '09  
GeneralGood Job Pinmembertclin15:44 19 Jan '09  
GeneralPlease Help ! PinmemberAksharRoop19:31 14 Jan '09  
GeneralDoesn't work for active scripting? PinmemberSusie248310:12 8 May '08  
QuestionThe remote server returned an error: (407) Proxy Authentication Required. Pinmembercheepau19:25 1 Oct '07  
GeneralSet cookie for root domain [SOLVED] PinmemberWebtijn5:13 10 Aug '07  
GeneralFinding TimeToFirstByte using HttpWebRequest PinmemberSakthiSurya22:16 15 Jul '07  
Generaltrying to log into netflix Pinmemberflipnjme18:57 15 Jun '07  
GeneralError from a web server on posting Pinmemberjwaldner8:58 25 May '07  
QuestionCan not read cookies from the local browser through HttpRequest PinmemberRanjit1620:52 16 Apr '07  
AnswerRe: Can not read cookies from the local browser through HttpRequest PinmemberMaruf Maniruzzaman2:17 12 May '07  
QuestionRe: Can not read cookies from the local browser through HttpRequest Pinmemberelcharro9:21 18 Jun '07  
Generallogin to website from code Pinmemberrama jayapal23:57 3 Apr '07  
GeneralRe: login to website from code PinmemberCPF_12:36 4 Apr '07  
GeneralRe: login to website from code Pinmemberrama jayapal22:18 6 Apr '07  
GeneralRe: login to website from code PinmemberCPF_3:20 7 Apr '07  
GeneralHelp me please PinmemberROZIK21:31 26 Mar '07  
NewsRe: Help me please PinmemberCPF_21:38 26 Mar '07  
NewsRe: Help me please PinmemberCPF_14:37 30 Mar '07  
GeneralRe: Help me please PinmemberROZIK19:42 30 Mar '07  
GeneralThanks a lotttttttttttttt Pinmembertrilokjain8:58 17 Mar '07  
AnswerRe: Thanks a lotttttttttttttt PinmemberCPF_9:04 17 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Mar 2007
Editor:
Copyright 2007 by CPF_
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project