How to add a custom header in HttpWebRequest
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:
- Universal Windows Platform 4.5
- .NET Framework 1.1
- Portable Class Library
- Silverlight 2.0
- Windows Phone Silverlight 7.0
- Windows Phone 8.1
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.