March 10, 2010   Login   |   Register  
  Search  
  |  
  Tips & Tricks | Developers Corner | HTTP Headers

HTTP Overview

Minimize

The HyperText Transfer Protocol (HTTP) is what we use for 99.999% of all web pages on the internet at the moment. Other protocols exist, like FTP, ARCHIE, GOPHER, and a lot more, but HTTP emerged as the premiere way to transfer information. Most of that what is transfered is either HTML or embedded inside of HTML documents.

A decent understanding of HTTP is necessary for any high-end or cutting-edge web design. A complete understanding of it is necessary when designing browsers or servers. The RFC for v1.1 can be found at ftp://ftp.isi.edu/in-notes/rfc2616.txt. More information is available at http://www.w3.org/Protocols/.

HTTP and HTML were originally developed by Tim Berners-Lee. You can find more information about him at http://www.w3.org/People/Berners-Lee/.

The scope here is limited to HTTP headers and how we can use them in designing more robust web applications. 

 Print   

Get all HTTP Headers

Minimize

If you do web design, and you don't know what HTTP headers are, then you need to know.

HTTP headers expose a great deal of information about your client as well as the server you are working, the application you are designing, as well as the environment you are in (SSL, etc.).

The functionality for this is held in "Request.ServerVariables", so you only need to access that. For example, Request.ServerVariables("ALL_HTTP") or Request.ServerVariables("HTTP_USER_AGENT"). You need to know the specific name of the header that you want. You can also create custom headers, but that's a subject that I'll cover later in another article.

Display all HTTP Headers in ASP

It is extremely simple to display all the HTTP headers in ASP. Here's a code snippit that will do it for you:

< %
for each header in Request.ServerVariables
 response.write header & " = " & Request.ServerVariables(header) & "  < br>< br> "
next
% >

Just delete the spaces near the angle brackets and run it on IIS. You'll get a list of all the HTTP headers along with the actual value for the header. Make sure to try this with different browsers and on different computers with different operating systems if you can. You'll immediately see the differences.

Display all HTTP Headers in ASP.NET

Doing it in .NET is little different. Have a look at the following code snippits.

Drag and drop a literal onto your aspx web form in design view. The HTML will have this in it:

< asp:literal id="Literal1" runat="server"></ asp:literal>

Now, in the code behind, add the following to the page load event:

        'Put user code to initialize the page here
        ' Create a string variable to hold our server variables
        Dim header As String
        ' Hold the values in a variable
        Dim holder As String

        ' Iterate through the servervariables collection and put the headers and values in a variable
        For Each header In Request.ServerVariables
            ' Store in our variable...
            holder &= (header.ToString & " = " & Request.ServerVariables(header).ToString & "  < br> < br>" & vbCrLf)
        Next
        ' Put our variable into our literal to display on our page
        Literal1.Text = holder

Run the page, and you'll get all the headers and values will be displayed.

As you can see, there is almost no difference between the ASP and ASP.NET code. In the ASP.NET, we just get the header information before we do anything in the page, then set a literal's value equal to that. When the page is displayed, the literal's value shows all the headers.

Download ASP and ASP.NET source code

Click here to download the source code in ZIP format to get and display all the various http headers available in ASP and ASP.NET.

Cheers,

Renegade

 

 Print   
     
Renegade Minds Web Media Basics  | 5.1 Audio on the Web | Email Avenger  | Web Form Submitter | HTTP Headers | HTTP_USER_AGENT

  Search

Copyright 2008 by Renegade Minds   |   Privacy Statement   |   Terms of Use
Renegade Minds