Created
April 12, 2017 10:55
-
-
Save mrstebo/81288a96f55c0b74f6c5b37ee17533a6 to your computer and use it in GitHub Desktop.
Extension method for adding a header to a HttpRequest.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Reflection; | |
using System.Web; | |
namespace MyApp | |
{ | |
internal static class HttpRequestHeaderExtensions | |
{ | |
public static void AddHeader(this HttpRequest request, string name, string value) | |
{ | |
var headers = request.Headers; | |
var hdr = headers.GetType(); | |
var ro = hdr.GetProperty("IsReadOnly", | |
BindingFlags.Instance | | |
BindingFlags.NonPublic | | |
BindingFlags.IgnoreCase | | |
BindingFlags.FlattenHierarchy); | |
ro.SetValue(headers, false, null); | |
hdr.InvokeMember("InvalidateCachedArrays", | |
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, | |
null, | |
headers, | |
null); | |
hdr.InvokeMember("BaseAdd", | |
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, | |
null, | |
headers, | |
new object[] {name, new ArrayList {value}}); | |
ro.SetValue(headers, true, null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment