Click here to Skip to main content
Licence MIT
First Posted 18 Mar 2009
Views 11,337
Bookmarked 5 times

A fast way to get a specific header value

By | 19 Mar 2009 | Article
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

About the Author

Scoby9



Canada Canada

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalok article PinmemberDonsw7:41 10 Apr '09  
GeneralThink a bit Pinmemberscalp2:22 20 Mar '09  
QuestionWhy? PinmemberZakPatat18:56 18 Mar '09  
AnswerRe: Why? PinmemberScoby919:54 18 Mar '09  
GeneralRe: Why? Pinmemberjgauffin21:12 18 Mar '09  
AnswerRe: Why? PinmemberScoby98:16 19 Mar '09  
GeneralRe: Why? Pinmemberjgauffin8:40 19 Mar '09  
AnswerRe: Why? PinmemberScoby98:48 19 Mar '09  
GeneralRe: Why? [modified] PinmemberZakPatat9:00 19 Mar '09  
GeneralRe: Why? PinmemberADLER110:37 19 Mar '09  
GeneralRe: Why? PinmemberZakPatat11:18 19 Mar '09  
GeneralRe: Why? PinmemberADLER112:12 19 Mar '09  
GeneralRe: Why? PinmemberZakPatat5:47 20 Mar '09  
GeneralRe: Why? PinmemberScoby98:24 20 Mar '09  
GeneralRe: Why? PinmemberTimMerksem6:14 24 Mar '09  
AnswerRe: Why? [modified] PinmemberScoby912:07 24 Mar '09  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 19 Mar 2009
Article Copyright 2009 by Scoby9
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid