Created
January 31, 2013 13:35
-
-
Save kntajus/4682906 to your computer and use it in GitHub Desktop.
Manually creating an HttpRequest for testing
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
// Whip up a fake HttpRequest | |
HttpRequest httpRequest = new HttpRequest("default.aspx", "http://www.diuturnal.com/default.aspx", string.Empty); | |
// Have to access the Headers collection first as this creates the internal collection we're about to hack | |
NameValueCollection headers = httpRequest.Headers; | |
// Accessing Headers property above will have created the private _headers member, so now we can grab it | |
headers = (NameValueCollection) httpRequest.GetType().GetField("_headers", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(httpRequest); | |
PropertyInfo readOnlyInfo = headers.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance); | |
// Hack the collection to not be read-only then add some headers | |
readOnlyInfo.SetValue(headers, false, null); | |
headers["Accept"] = "text/xml,application/xml,application/xhtml+xml,text/html; | |
q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; | |
headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2"; | |
// Finished hacking, set it back to read-only | |
readOnlyInfo.SetValue(headers, true, null); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment