How to add a custom header in HttpWebRequest

#c-sharp #stackoverflow

Written by Anders Marzi Tornblad

StackOverflow user Nelson T Joseph needed to add custom headers to a HttpWebRequest object, and wanted to know how to do that in code targetting Windows Phone 7.

The solution is to add to the Headers property using a string index:

request.Headers["X-My-Custom-Header"] = "the-value";

According to official documentation, this has been possible since:

Just keep in mind that this works best for custom headers, and that standard headers should be handled using other properties on the HttpWebRequest object.