简体   繁体   English

如何绕过受限的 HTTPWebRequest 标头

[英]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我做了一个小辅助函数来处理我的 HTTP 请求,它只需要一个字符串 URL 和一个格式为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?但是,如果 Headers 数组包含受限标头(例如 User-Agent、Content-Type 或 Accept 等),我会收到一个异常,告诉我使用其属性或方法修改标头,所以我在想也许有某种方法来检查是否有任何标题受到限制并使用其属性自动修改它,不幸的是,我不太聪明,所以我真的不知道如何做到这一点,而无需大量臃肿的代码检查每个受限制的标题并为每个人运行不同的代码我猜它可以用反射来完成,但我不确定......帮助?

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.或者单独处理每个标题,因为它们只有 11 个,而不是一百个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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