Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#

HttpWebRequest/Response in a Nutshell - Part 1

Rate me:
Please Sign up or sign in to vote.
4.69/5 (39 votes)
15 Mar 2007CPOL2 min read 399.4K   133   31
Using HttpWebRequest/Response to do basic web surfing

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.MethodUsed to set the method ("GET" or "POST") of retrieving 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 initialize 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 write namespaces. Here we will use:

C#
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).

C#
private static void start_post()
{
    //Our postvars
    byte[]  buffer = Encoding.ASCII.GetBytes( "test=postvar&test2=another" );
    //Initialization, we use localhost, change if applicable
    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.

C#
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";
    //Initialization, we use localhost, change if applicable
    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? :)

C#
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);
            //Initialization
            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)
        {
            //Initialization
            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
<?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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Belgium Belgium
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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Bill Warner6-Oct-15 6:48
Bill Warner6-Oct-15 6:48 
Questionuseless Pin
logistum17-Mar-13 12:51
logistum17-Mar-13 12:51 
Questionfor calculator page Pin
Michael Pious31-Jan-13 19:29
Michael Pious31-Jan-13 19:29 
QuestionHow to call a aspx web application in windows application? Pin
Ganesh KP8-Oct-12 0:06
professionalGanesh KP8-Oct-12 0:06 
GeneralMy vote of 5 Pin
jigneshpatelit6119895-Apr-11 4:21
jigneshpatelit6119895-Apr-11 4:21 
GeneralMy vote of 5 Pin
Lisa Malenfant16-Mar-11 13:10
Lisa Malenfant16-Mar-11 13:10 
GeneralMy vote of 5 Pin
Amir Mehrabi-Jorshari10-Dec-10 9:59
Amir Mehrabi-Jorshari10-Dec-10 9:59 
Generalneed help for paging Pin
aamirzada8-Oct-09 21:42
aamirzada8-Oct-09 21:42 
Generalhandling session Pin
aamirzada30-Jun-09 22:50
aamirzada30-Jun-09 22:50 
GeneralGood Job Pin
tclin19-Jan-09 14:44
tclin19-Jan-09 14:44 
GeneralPlease Help ! Pin
AksharRoop14-Jan-09 18:31
AksharRoop14-Jan-09 18:31 
QuestionDoesn't work for active scripting? Pin
Susie24838-May-08 9:12
Susie24838-May-08 9:12 
QuestionThe remote server returned an error: (407) Proxy Authentication Required. Pin
cheepau1-Oct-07 18:25
cheepau1-Oct-07 18:25 
AnswerRe: The remote server returned an error: (407) Proxy Authentication Required. Pin
KalyanGS9-Aug-09 22:15
KalyanGS9-Aug-09 22:15 
GeneralSet cookie for root domain [SOLVED] Pin
Webtijn10-Aug-07 4:13
Webtijn10-Aug-07 4:13 
GeneralFinding TimeToFirstByte using HttpWebRequest Pin
SakthiSurya15-Jul-07 21:16
SakthiSurya15-Jul-07 21:16 
Generaltrying to log into netflix Pin
flipnjme15-Jun-07 17:57
flipnjme15-Jun-07 17:57 
GeneralError from a web server on posting Pin
jwaldner25-May-07 7:58
jwaldner25-May-07 7:58 
QuestionCan not read cookies from the local browser through HttpRequest Pin
Ranjit1616-Apr-07 19:52
Ranjit1616-Apr-07 19:52 
AnswerRe: Can not read cookies from the local browser through HttpRequest Pin
Maruf Maniruzzaman12-May-07 1:17
Maruf Maniruzzaman12-May-07 1:17 
QuestionRe: Can not read cookies from the local browser through HttpRequest Pin
elcharro18-Jun-07 8:21
elcharro18-Jun-07 8:21 
Generallogin to website from code Pin
rama jayapal3-Apr-07 22:57
rama jayapal3-Apr-07 22:57 
GeneralRe: login to website from code Pin
CPF_4-Apr-07 11:36
CPF_4-Apr-07 11:36 
GeneralRe: login to website from code Pin
rama jayapal6-Apr-07 21:18
rama jayapal6-Apr-07 21:18 
GeneralRe: login to website from code Pin
CPF_7-Apr-07 2:20
CPF_7-Apr-07 2:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.