---
title: "Explain the HttpRequest Class in C#."  
description: "Explain the HttpRequest Class in C#."  
author: "ICSM Computer"  
published: 2025-05-05  
updated: 2025-05-05  
canonical: https://www.mindstick.com/interview/34079/explain-the-httprequest-class-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 5 minutes  

---

# Explain the HttpRequest Class in C#.

The `HttpRequest` class in C# (specifically in **ASP.NET**) represents the **incoming HTTP request** from the client (browser) to the server. It is used to access **form data, query strings, headers, cookies, request type**, and other request-related info.

## Namespace

```cs
using System.Web;
```

## You access it in ASP.NET MVC via:

```cs
Request  // This is an instance of HttpRequestBase
```

In Web Forms:

```cs
HttpContext.Current.Request
```

## Class Overview

```cs
public sealed class HttpRequest
```

This class **encapsulates all HTTP-specific information about a single HTTP request**.

## Common Properties

| Property | Description |
| --- | --- |
| `HttpMethod` | Returns `GET`, `POST`, `PUT`, `DELETE`, etc. |
| `Url` | Full URL of the request |
| `RawUrl` | The raw URL as requested by the client |
| `QueryString` | Collection of query string values (`?id=5`) |
| `Form` | Collection of form data (from POST body) |
| `Headers` | Collection of request headers |
| `Cookies` | Cookies sent by the browser |
| `UserAgent` | Info about the browser or client |
| `ContentType` | The MIME type of the request body |
| `InputStream` | The raw binary input stream (e.g., for file uploads) |
| `Files` | Collection of uploaded files |
| `IsAuthenticated` | Indicates whether the user is authenticated |

## Examples

## Read Query String Value

```cs
string id = Request.QueryString["id"]; // from ?id=123
```

## Read Form Data (POST)

```cs
string username = Request.Form["username"];
```

## Get Full URL and Method

```cs
string fullUrl = Request.Url.ToString();
string method = Request.HttpMethod;  // GET or POST etc.
```

## Get Client IP Address

```cs
string ip = Request.UserHostAddress;
```

> Or if using reverse proxy:

```cs
string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.UserHostAddress;
```

## Access Uploaded File

```cs
HttpPostedFile file = Request.Files["upload"];
if (file != null && file.ContentLength > 0)
{
    string filename = Path.GetFileName(file.FileName);
    file.SaveAs(Server.MapPath("~/Uploads/" + filename));
}
```

## Access Request Headers

```cs
string token = Request.Headers["Authorization"];
```

## Use in ASP.NET MVC

In MVC, `Request` is of type `HttpRequestBase`, which is an abstracted/testable version of `HttpRequest`.

You can still do:

```cs
public ActionResult Example()
{
    string userAgent = Request.UserAgent;
    string referer = Request.UrlReferrer?.ToString();
    return View();
}
```

## Summary Table

| Use Case | Code |
| --- | --- |
| Get method type | `Request.HttpMethod` |
| Read query string | `Request.QueryString["key"]` |
| Read form field | `Request.Form["field"]` |
| Uploaded file | `Request.Files["name"]` |
| Client IP | `Request.UserHostAddress` |
| Headers | `Request.Headers["HeaderName"]` |
| Cookies | `Request.Cookies["cookieName"]` |

## Answers

### Answer by ICSM Computer

The `HttpRequest` class in C# (specifically in **ASP.NET**) represents the **incoming HTTP request** from the client (browser) to the server. It is used to access **form data, query strings, headers, cookies, request type**, and other request-related info.

## Namespace

```cs
using System.Web;
```

## You access it in ASP.NET MVC via:

```cs
Request  // This is an instance of HttpRequestBase
```

In Web Forms:

```cs
HttpContext.Current.Request
```

## Class Overview

```cs
public sealed class HttpRequest
```

This class **encapsulates all HTTP-specific information about a single HTTP request**.

## Common Properties

| Property | Description |
| --- | --- |
| `HttpMethod` | Returns `GET`, `POST`, `PUT`, `DELETE`, etc. |
| `Url` | Full URL of the request |
| `RawUrl` | The raw URL as requested by the client |
| `QueryString` | Collection of query string values (`?id=5`) |
| `Form` | Collection of form data (from POST body) |
| `Headers` | Collection of request headers |
| `Cookies` | Cookies sent by the browser |
| `UserAgent` | Info about the browser or client |
| `ContentType` | The MIME type of the request body |
| `InputStream` | The raw binary input stream (e.g., for file uploads) |
| `Files` | Collection of uploaded files |
| `IsAuthenticated` | Indicates whether the user is authenticated |

## Examples

## Read Query String Value

```cs
string id = Request.QueryString["id"]; // from ?id=123
```

## Read Form Data (POST)

```cs
string username = Request.Form["username"];
```

## Get Full URL and Method

```cs
string fullUrl = Request.Url.ToString();
string method = Request.HttpMethod;  // GET or POST etc.
```

## Get Client IP Address

```cs
string ip = Request.UserHostAddress;
```

> Or if using reverse proxy:

```cs
string ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.UserHostAddress;
```

## Access Uploaded File

```cs
HttpPostedFile file = Request.Files["upload"];
if (file != null && file.ContentLength > 0)
{
    string filename = Path.GetFileName(file.FileName);
    file.SaveAs(Server.MapPath("~/Uploads/" + filename));
}
```

## Access Request Headers

```cs
string token = Request.Headers["Authorization"];
```

## Use in ASP.NET MVC

In MVC, `Request` is of type `HttpRequestBase`, which is an abstracted/testable version of `HttpRequest`.

You can still do:

```cs
public ActionResult Example()
{
    string userAgent = Request.UserAgent;
    string referer = Request.UrlReferrer?.ToString();
    return View();
}
```

## Summary Table

| Use Case | Code |
| --- | --- |
| Get method type | `Request.HttpMethod` |
| Read query string | `Request.QueryString["key"]` |
| Read form field | `Request.Form["field"]` |
| Uploaded file | `Request.Files["name"]` |
| Client IP | `Request.UserHostAddress` |
| Headers | `Request.Headers["HeaderName"]` |
| Cookies | `Request.Cookies["cookieName"]` |


---

Original Source: https://www.mindstick.com/interview/34079/explain-the-httprequest-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
