This documentation is archived and is not being maintained.

HttpRequest Class

Updated: April 2009

Enables ASP.NET to read the HTTP values sent by a client during a Web request.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class HttpRequest

The methods and properties of the HttpRequest class are exposed through the Request properties of the HttpApplication, HttpContext, Page, and UserControl classes.


Unicode support for HttpRequest class members requires IIS version 6.0 or later.

How to: Pass Values Between ASP.NET Web PagesBuilding ASP .NET Web Applications
How to: Pass Values Between ASP.NET Web PagesBuilding ASP .NET Web Applications

The following example uses the StreamWriter class to write the values of several HttpRequest class properties to a file. For properties that are of type string, the values are HTML encoded as they are written to the file. Properties that represent a collection are looped through, and each key/value pair that they contain is written to the file. In ASP.NET markup, the Request keyword refers to the HttpRequest object.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Page Language="C#" %>
<%@ import Namespace="System.Threading" %>
<%@ import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<script runat="server">

    /* NOTE: To use this sample, create a c:\temp\CS folder,
    *  add the ASP.NET account (in IIS 5.x <machinename>\ASPNET,
    *  in IIS 6.x NETWORK SERVICE), and give it write permissions
    *  to the folder.*/ 

    private const string INFO_DIR = @"c:\temp\CS\RequestDetails";
    public static int requestCount;

    private void Page_Load(object sender, System.EventArgs e)

        // Create a variable to use when iterating 
        // through the UserLanguages property. 
        int langCount;

        int requestNumber = Interlocked.Increment(ref requestCount);

        // Create the file to contain information about the request.
        string strFilePath = INFO_DIR + requestNumber.ToString() + @".txt";

        StreamWriter sw = File.CreateText(strFilePath);

            // Write request information to the file with HTML encoding.

            // Iterate through the Form collection and write 
            // the values to the file with HTML encoding. 
            // String[] formArray = Request.Form.AllKeys; 
            foreach (string s in Request.Form)
                sw.WriteLine("Form: " + Server.HtmlEncode(s));

            // Write the PathInfo property value 
            // or a string if it is empty. 
            if (Request.PathInfo == String.Empty)
                sw.WriteLine("The PathInfo property contains no information.");

            // Write request information to the file with HTML encoding.

            // Write a message to the file dependent upon 
            // the value of the TotalBytes property. 
            if (Request.TotalBytes > 1000)
                sw.WriteLine("The request is 1KB or greater");
                sw.WriteLine("The request is less than 1KB");

            // Write request information to the file with HTML encoding.

            // Iterate through the UserLanguages collection and 
            // write its HTML encoded values to the file. 
            for (langCount=0; langCount < Request.UserLanguages.Length; langCount++)
                sw.WriteLine(@"User Language " + langCount +": " + Server.HtmlEncode(Request.UserLanguages[langCount]));

            // Close the stream to the file.

        lblInfoSent.Text = "Information about this request has been sent to a file.";

    private void btnSendInfo_Click(object sender, System.EventArgs e)
        lblInfoSent.Text = "Hello, " + Server.HtmlEncode(txtBoxName.Text) +
          ". You have created a new  request info file.";

<html xmlns="" >
    <title>ASP.NET Example</title>
    <form id="form1" runat="server">
            Enter your name here:
            <asp:TextBox id="txtBoxName" runat="server"></asp:TextBox>
            <asp:Button id="btnSendInfo" onclick="btnSendInfo_Click" runat="server" Text="Click Here"></asp:Button>
            <asp:Label id="lblInfoSent" runat="server"></asp:Label>


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0




April 2009

Added a note explaining that the Request keyword in markup refers to the HttpRequest object.

Customer feedback.