I'm currently parsing some HTTP request headers from a log file, I need to split them up and create a dictionary for easier lookups. The code I'm using is:
public static Dictionary<string, string> CreateLookupDictionary(string input)
{
Debug.WriteLine(input);
return input.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(new string[] {": "}, StringSplitOptions.None))
.ToDictionary(x => x[0], x => x[1], StringComparer.InvariantCultureIgnoreCase);
}
This is working for 99% of the headers, but then...
...
Keep-Alive: timeout=20
Expires: Sat, 04 Jun 2011 18:43:08 GMT
Cache-Control: max-age=31536000
Cache-Control: public
Accept-Ranges: bytes
...
Now the key Cache-Control
already exists, so it's throwing an exception about the key already existing.
Is there an elegant way to overwrite the value that's there, I don't want to have to rewrite the LINQ unless I really have to.
Thanks