How can I Bypass restricted HTTPWebRequest Headers

Viewed 254

I've made a little helper function to deal with my HTTP requests that simply takes a string URL and a string array of headers formatted as Header-Name: Value

public static HttpWebResponse MakeHttpRequest(string URL, string[] Headers)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
    req.Headers.Clear();
    foreach (string h in Headers)
    {
        var s = h.Split(new char[] { ':' }, 2);
        req.Headers.Set(s[0], s[1]);
    }
    return (HttpWebResponse)req.GetResponse();
}

But if the Headers array contains a Restricted header (e.g User-Agent, or Content-Type, or Accept, etc..) I get an exception telling me to modify the header using its property or method, so I was thinking maybe there was some way to check if any of the Headers was restricted and automatically modify it using its property, unfortunately, I'm not too smart so I don't really know how to do that without having a lot of bloated code checking every single restricted header and having different code run for each one Im guessing it can be done with Reflection but I'm not sure... Help?

1 Answers

There is static method to check if header is restricted:

bool restricted = System.Net.WebHeaderCollection.IsRestricted(key); // key = s[0]

You can use reflection to set related property like this:

public static HttpWebResponse MakeHttpRequest(string URL, string[] Headers) {
    HttpWebRequest req = (HttpWebRequest) WebRequest.Create(URL);
    req.Headers.Clear();
    foreach (string h in Headers) {
        var s = h.Split(new char[] {':'}, 2);
        var key = s[0];
        var value = s[1];
        if (WebHeaderCollection.IsRestricted(key)) {
            // remove "-" because some header names contain it, but .NET properties do not
            key = key.Replace("-", "");
            // get property with header name
            var prop = typeof(HttpWebRequest).GetProperty(key, BindingFlags.Instance | BindingFlags.Public);
            // set, changing type if necessary (some properties are long, or date, and your values are always strings)
            prop.SetValue(req, Convert.ChangeType(value, prop.PropertyType, CultureInfo.InvariantCulture));
        }
        else {
            req.Headers.Set(s[0], s[1]);
        }

    }

    return (HttpWebResponse) req.GetResponse();
}

Ensure to test this code against all restricted headers (listed here) if you are going to use it, because I did not. Or just handle each header separately, because there are just 11 of them, not a hundred.

Related