70

I'd like to email myself a quick dump of a GET request's headers for debugging. I used to be able to do this in classic ASP simply with the Request object, but Request.ToString() doesn't work. And the following code returned an empty string:

using (StreamReader reader = new StreamReader(Request.InputStream))
{
    string requestHeaders = reader.ReadToEnd();
    // ...
    // send requestHeaders here
}

7 Answers 7

132

Have a look at the Headers property in the Request object.

C#

string headers = Request.Headers.ToString();

Or, if you want it formatted in some other way:

string headers = String.Empty;
foreach (var key in Request.Headers.AllKeys)
  headers += key + "=" + Request.Headers[key] + Environment.NewLine;

VB.NET:

Dim headers = Request.Headers.ToString()

Or:

Dim headers As String = String.Empty
For Each key In Request.Headers.AllKeys
  headers &= key & "=" & Request.Headers(key) & Environment.NewLine
Next
9
  • +1 Just add a line to email it and I think this is the full answer (the question was tagged C# so I don't think the VB.Net version is essential).
    – amelvin
    Apr 13, 2010 at 10:11
  • First KeyValuePair snippet caused runtime cast error so I'm using foreach (string key in Request.Headers) header += key + " = " + Request.Headers[key] + Environment.NewLine; Apr 13, 2010 at 10:25
  • 3
    You may join all data using string.Join method: string.Join(Environment.NewLine, Request.Headers.AllKeys.Select(key=>string.Format("Key:{0}, Value:{1}", key, Request.Headers[key]))); This method is faster then your because string.Join is more effective to join several objects Jan 22, 2013 at 18:43
  • For AspNetCore, headers are wrapped because of Kestrel and the key is prefixed with 'Header', so you would need to iterate the header dictionary and then dump the key-value-pairs, probably removing the word 'Header'. Jul 24, 2017 at 12:01
  • 3
    In ASP.NET Core 3.1 Request.Headers.ToString() returns data type name Microsoft.AspNetCore.HttpSys.Internal.RequestHeaders and Request.Headers has no AllKeys member.
    – Paul
    Mar 1, 2022 at 18:42
30

You could turn on tracing on the page to see headers, cookies, form variables, querystring etc painlessly:

Top line of the aspx starting:

<%@ Page Language="C#" Trace="true" 
1
  • 4
    Trace not available for this configuration: <deployment retail=true /> is typically used in production web servers in machine.config you can read more about config values inheritance from here msdn.microsoft.com/en-us/library/ms178685.aspx
    – Kiquenet
    Nov 16, 2015 at 14:25
5

For those (like me) having troubles with the absence of AllKeys property in IHeaderDictionary implementation, this is the way I was able to serialize all headers in a string (inside a controller action).

using System;
using System.Text;

// ...

var builder = new StringBuilder(Environment.NewLine);
foreach (var header in Request.Headers)
{
    builder.AppendLine($"{header.Key}: {header.Value}");
}
var headersDump = builder.ToString();

I'm using ASP.NET Core 3.1.

2

asp.net core spits out Microsoft.AspNetCore.HttpSys.Internal.RequestHeaders for Request.Headers.ToString(), so the solution in that context is:

IEnumerable<string> keyValues = context.Request.Headers.Keys.Select(key => key + ": " + string.Join(",", context.Request.Headers[key]));
string requestHeaders = string.Join(System.Environment.NewLine, keyValues);
2

.Net 6.0 and above:

public virtual IActionResult Headers()
{
    System.Text.StringBuilder sb = new();
    foreach (var header in _httpContext.HttpContext.Request.Headers)
    {
        sb.Append(header.Key);
        sb.Append("=");
        sb.AppendLine(header.Value);
    }
    return Ok(sb.ToString());
}
0

You can use,

string headers = Request.Headers.ToString(); 

But It will return URL encoded string so to decode it use below code,

String headers = HttpUtility.UrlDecode(Request.Headers.ToString()) 
1
  • 3
    For me, it outputs only Microsoft.AspNetCore.HttpSys.Internal.RequestHeaders instead of actual headers. Aug 12, 2021 at 6:41
0

You can get all headers as string() in one shot, using this (VB.Net)

Request.Headers.ToString.Split(New String() {vbCrLf}, StringSplitOptions.RemoveEmptyEntries)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.