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

A fast way to get a specific header value

By , 19 Mar 2009
 

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalok articlememberDonsw10 Apr '09 - 7:41 
GeneralThink a bitmemberscalp20 Mar '09 - 2:22 
QuestionWhy?memberZakPatat18 Mar '09 - 18:56 
AnswerRe: Why?memberScoby918 Mar '09 - 19:54 
GeneralRe: Why?memberjgauffin18 Mar '09 - 21:12 
AnswerRe: Why?memberScoby919 Mar '09 - 8:16 
GeneralRe: Why?memberjgauffin19 Mar '09 - 8:40 
AnswerRe: Why?memberScoby919 Mar '09 - 8:48 
GeneralRe: Why? [modified]memberZakPatat19 Mar '09 - 9:00 
GeneralRe: Why?memberADLER119 Mar '09 - 10:37 
GeneralRe: Why?memberZakPatat19 Mar '09 - 11:18 
GeneralRe: Why?memberADLER119 Mar '09 - 12:12 
GeneralRe: Why?memberZakPatat20 Mar '09 - 5:47 
GeneralRe: Why?memberScoby920 Mar '09 - 8:24 
GeneralRe: Why?memberTimMerksem24 Mar '09 - 6:14 
AnswerRe: Why? [modified]memberScoby924 Mar '09 - 12:07 

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

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