65.9K
CodeProject is changing. Read more.
Home

A fast way to get a specific header value

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (1 vote)

Mar 18, 2009

MIT
viewsIcon

27784

A fast way to get a specific header value.

Introduction

This article explains a fast way to get a specific header value. This is just a more generic version of http://dotnetperls.com/Content/GZIP-Request.aspx.

Although my method is slightly slower (performance difference is almost unnoticeable), this way, you don't need to hard-code for all possible headers. I also tried comparing a char using a loop but it's slower.

Using the code

public static string GetHeaderValue(NameValueCollection headers, string key)
{
    for (int i = 0; i < headers.Count; i++)
    {
        if (string.Equals(headers.GetKey(i), key, 
            System.StringComparison.OrdinalIgnoreCase))
        {
            return headers.Get(i);
        }
    }

    return string.Empty;
}

For example, if you want to find "Accept-Encoding", just call GetHeaderValue(request.Headers, "Accept-Encoding"), assuming you have an instance of an HttpRequest called request.

Performance test:

Just put the following code into a console program:

private static NameValueCollection headers = new NameValueCollection();
        
static void Main(string[] args)
{            
    Console.ReadLine();
    GetHeaderValuePerformanceTest();
    Console.ReadLine();
}

static void GetHeaderValuePerformanceTest()
{
    
    headers.Add("Content-Type", "application/json");
    headers.Add("Accept", "text/html,application/xhtml+xml," + 
                "application/xml;q=0.9,*/*;q=0.8");
    headers.Add("Accept-Language", "en-us,en;q=0.5");
    headers.Add("Accept-Encoding", "gzip");
    headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    headers.Add("Connection", "keep-alive");

    int iterations = 1000000;
    Stopwatch watch;

    // GetHeaderValue
    watch = Stopwatch.StartNew();
    for (int i = 0; i < iterations; i++)
    {
        var result = GetHeaderValue(headers, "Accept-Encoding");
    }
    watch.Stop();
    Console.Write("GetHeaderValue: " + watch.ElapsedMilliseconds.ToString());
    Console.WriteLine();

    // Indexer
    watch = Stopwatch.StartNew();
    for (int i = 0; i < iterations; i++)
    {
        var result = headers["Accept-Encoding"];
    }

    watch.Stop();
    Console.Write("Indexer: " + watch.ElapsedMilliseconds.ToString());
    Console.WriteLine();
}

static string GetHeaderValue(NameValueCollection headers, string key)
{
    for (int i = 0; i < headers.Count; i++)
    {
        if (string.Equals(headers.GetKey(i), key, 
            System.StringComparison.OrdinalIgnoreCase))
        {
            return headers.Get(i);
        }
    }

    return string.Empty;
}

Results of 1000000 loops:

GetHeaderValue:344ms
Indexer:1350ms

Points of interest

This method will be soon added to my project Apprender.Common 2.6 hosted at http://apprender.codeplex.com/.

History

  • March 17, 2009: Posted.
  • March 18, 2009: Added Performance Test, updated code.