Home
Map
Request.HeadersAnalyze HTTP headers from Requests by accessing Request.Headers.
ASP.NET
This page was last reviewed on Sep 27, 2022.
Request.Headers. This provides useful information. With the data in this collection, we can decide how to render a response. And we can generate statistics about the visitors of a website.
Benefits. You can benefit from analyzing HTTP headers used with your site. The code will also let you see what cookies are being sent to your application.
Example. This code example shows how to access the collection of HTTP headers in ASP.NET. It uses Page_Load and the NameValueCollection.
Info When you place the code in a code-behind file and execute it, you will see all the headers in the Request.Headers collection.
using System; using System.Web.UI; using System.Collections.Specialized; public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { NameValueCollection headers = base.Request.Headers; for (int i = 0; i < headers.Count; i++) { string key = headers.GetKey(i); string value = headers.Get(i); base.Response.Write(key + " = " + value + "<br/>"); } } }
Record values. You can add a C# code file to the App_Code folder in your ASP.NET project. This code will record each individual value for each key in the Headers collection.
Tip You must call it on each Request, either in Global.asax or your Page_Load method.
Detail This is a public static class that contains a static List of HeaderData instances. It defines a nested class called HeaderData.
Detail This is a public static method that receives a HttpRequest instance from the System.Web namespace.
Info Record() enumerates through the Headers collection. It records each individual header from the Request into a new HeaderData.
using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using System.Web; public static class HeaderStatTool { static List<HeaderData> _list = new List<HeaderData>(); class HeaderData { public string Key { get; set; } public int Index { get; set; } public string Value { get; set; } }; public static void Record(HttpRequest q) { try { NameValueCollection headers = q.Headers; if (headers != null) { for (int i = 0; i < headers.Count; i++) { _list.Add(new HeaderData() { Key = headers.GetKey(i), Index = i, Value = headers.Get(i) }); } } } catch { } } }
Diagnostics. This property uses some logic with List and Dictionary to report the important parts of the HTTP headers sent to your application. It should be a method, not a property.
Info Diagnostics loops through and tabulates the header data instances in the List. It builds up a StringBuilder of the final report.
And The method reports each HTTP header found in ASP.NET, and prints the number of times it was encountered.
Next The program prints the indexes of each HTTP header and how often each index was found.
Finally It prints out all the values encountered and the number of times they were used.
public static string Diagnostics { get { // 1: Store results in StringBuilder StringBuilder builder = new StringBuilder(); try { // 2: Get all names of headers var nameDictionary = new Dictionary<string, bool>(); foreach (HeaderData data in _list) { if (!nameDictionary.ContainsKey(data.Key)) { nameDictionary.Add(data.Key, true); } } // 3: Loop through names foreach (string name in nameDictionary.Keys) { try { // 4: Write name and count of the name var nameList = _list.FindAll(item => item.Key == name); builder.AppendFormat("Header: {0} = {1} items\n", name, nameList.Count); // 5: Count index frequencies for (int i = 0; i < 10; i++) { int count = nameList.FindAll(item => item.Index == i).Count; if (count != 0) { builder.AppendFormat(" Index: {0} = {1} items\n", i, count); } } // 6: Get frequencies of values var valueDictionary = new Dictionary<string, int>(); foreach (HeaderData data in nameList) { if (valueDictionary.ContainsKey(data.Value)) { valueDictionary[data.Value]++; } else { valueDictionary.Add(data.Value, 1); } } // 7: Write values and frequencies foreach (var keyValuePair in valueDictionary) { builder.AppendFormat(" Value: {0} = {1} count\n", keyValuePair.Key, keyValuePair.Value); } } catch { builder.AppendFormat("Error: {0}\n", name); } } } catch { builder.Append("Error\n"); } // 8: Return the result string return builder.ToString(); } }
Header: Host = 1730 items Index: 0 = 4 items Index: 1 = 25 items Index: 2 = 42 items Index: 3 = 44 items Index: 4 = 642 items Index: 5 = 375 items Index: 6 = 496 items Index: 7 = 94 items Index: 8 = 3 items Index: 9 = 5 items Value: dotnetperls.com = 1714 count Value: www.dotnetperls.com = 16 count Header: Accept-Encoding = 740 items Index: 1 = 11 items Index: 2 = 265 items Index: 3 = 175 items Index: 4 = 276 items Index: 5 = 12 items Index: 6 = 1 items Value: gzip, deflate = 282 count Value: gzip,deflate,bzip2,sdch = 95 count Value: gzip,deflate = 227 count Value: deflate, gzip, x-gzip, identity, *;q=0 = 10 count Value: gzip, x-gzip = 10 count Value: gzip = 106 count Value: identity = 3 count Value: gzip, deflate, identity = 6 count Value: gzip,identity = 1 count
Summary. We developed code to add live reporting of the kinds of HTTP requests that are accessing an ASP.NET server. This helps with debugging and can improve a website.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 27, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.