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.
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