74

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

135

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
Sign up to request clarification or add additional context in comments.

9 Comments

+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).
First KeyValuePair snippet caused runtime cast error so I'm using foreach (string key in Request.Headers) header += key + " = " + Request.Headers[key] + Environment.NewLine;
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
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'.
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.
|
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 Comment

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
5

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);

Comments

4

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.

1 Comment

.NET core is not relevant to a question about the original ASP.NET
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());
}

Comments

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 Comment

For me, it outputs only Microsoft.AspNetCore.HttpSys.Internal.RequestHeaders instead of actual headers.
0

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

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.