Click here to Skip to main content
11,496,146 members (716 online)
Click here to Skip to main content

A fast way to get a specific header value

, 19 Mar 2009 MIT 16.1K 6
A fast way to get a specific header value.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

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 Pin
Donsw10-Apr-09 8:41
memberDonsw10-Apr-09 8:41 
GeneralThink a bit Pin
scalp20-Mar-09 3:22
memberscalp20-Mar-09 3:22 
QuestionWhy? Pin
ZakPatat18-Mar-09 19:56
memberZakPatat18-Mar-09 19:56 
AnswerRe: Why? Pin
Scoby918-Mar-09 20:54
memberScoby918-Mar-09 20:54 
GeneralRe: Why? Pin
jgauffin18-Mar-09 22:12
memberjgauffin18-Mar-09 22:12 
AnswerRe: Why? Pin
Scoby919-Mar-09 9:16
memberScoby919-Mar-09 9:16 
GeneralRe: Why? Pin
jgauffin19-Mar-09 9:40
memberjgauffin19-Mar-09 9:40 
AnswerRe: Why? Pin
Scoby919-Mar-09 9:48
memberScoby919-Mar-09 9:48 
GeneralRe: Why? [modified] Pin
ZakPatat19-Mar-09 10:00
memberZakPatat19-Mar-09 10:00 
GeneralRe: Why? Pin
ADLER119-Mar-09 11:37
memberADLER119-Mar-09 11:37 
GeneralRe: Why? Pin
ZakPatat19-Mar-09 12:18
memberZakPatat19-Mar-09 12:18 
GeneralRe: Why? Pin
ADLER119-Mar-09 13:12
memberADLER119-Mar-09 13:12 
GeneralRe: Why? Pin
ZakPatat20-Mar-09 6:47
memberZakPatat20-Mar-09 6:47 
GeneralRe: Why? Pin
Scoby920-Mar-09 9:24
memberScoby920-Mar-09 9:24 
GeneralRe: Why? Pin
TimMerksem24-Mar-09 7:14
memberTimMerksem24-Mar-09 7:14 
AnswerRe: Why? [modified] Pin
Scoby924-Mar-09 13:07
memberScoby924-Mar-09 13: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 | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 19 Mar 2009
Article Copyright 2009 by Scoby9
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid