Click here to Skip to main content
Click here to Skip to main content

A fast way to get a specific header value

, 19 Mar 2009 MIT
Rate this:
Please Sign up or sign in to vote.
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.

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Scoby9

Canada Canada
My Projects:
- Apprender.Common http://apprender.codeplex.com/
 
Education History:
- Junior High: YUSS
- Senior High: Harry Ainlay
- University: undecided

Comments and Discussions

 
Generalok article PinmemberDonsw10-Apr-09 7:41 
GeneralThink a bit Pinmemberscalp20-Mar-09 2:22 
QuestionWhy? PinmemberZakPatat18-Mar-09 18:56 
AnswerRe: Why? PinmemberScoby918-Mar-09 19:54 
GeneralRe: Why? Pinmemberjgauffin18-Mar-09 21:12 
AnswerRe: Why? PinmemberScoby919-Mar-09 8:16 
GeneralRe: Why? Pinmemberjgauffin19-Mar-09 8:40 
AnswerRe: Why? PinmemberScoby919-Mar-09 8:48 
GeneralRe: Why? [modified] PinmemberZakPatat19-Mar-09 9:00 
GeneralRe: Why? PinmemberADLER119-Mar-09 10:37 
GeneralRe: Why? PinmemberZakPatat19-Mar-09 11:18 
GeneralRe: Why? PinmemberADLER119-Mar-09 12:12 
GeneralRe: Why? PinmemberZakPatat20-Mar-09 5:47 
GeneralRe: Why? PinmemberScoby920-Mar-09 8:24 
GeneralRe: Why? PinmemberTimMerksem24-Mar-09 6:14 
AnswerRe: Why? [modified] PinmemberScoby924-Mar-09 12:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 19 Mar 2009
Article Copyright 2009 by Scoby9
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid