_webRequest is a HttpWebRequest instance, _headers is a WebHeaderCollection from the original HttpWebRequest. I am relaying headers from one request to another, as required by a third party application. Why do I have to write such terrible code?
foreach (var key in _headers.AllKeys)
{
// of course MS would have to fuck this up somehow
var value = _headers[key];
Log.Debug(key + ": " + value);
switch (key.ToUpper())
{
case "ACCEPT": _webRequest.Accept = value; break;
case "REFERER": _webRequest.Referer = value; break;
case "USER-AGENT": _webRequest.UserAgent = value; break;
case "TRANSFER-ENCODING": _webRequest.UserAgent = value; break;
case "DATE":
if (DateTime.TryParse(value, out dt))
_webRequest.Date = dt;
break;
case "IF-MODIFIED-SINCE":
if (DateTime.TryParse(value, out dt))
_webRequest.IfModifiedSince = dt;
break;
case "CONTENT-LENGTH":
int cl;
if (Int32.TryParse(value, out cl))
_webRequest.ContentLength = cl;
break;
default:
_webRequest.Headers.Add(key, _headers[key]);
break;
}
}
It should be what I originally tried:
//_webRequest.Headers.Add(_headers);
This is precisely the sort of code that creates a great dissatisfaction within me when working with the .NET Framework.