简体   繁体   中英

How can I Bypass restricted HTTPWebRequest Headers

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 (eg 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM