Asp.NET 1.1 HttpResponse标头

时间:2010-04-12 20:22:13

标签: asp.net http-headers httpresponse

我有Asp.NET 1.1项目的一部分。

我使用远程站点,在某些情况下工作不正确 - 有时它会写错误的Content-Encoding标头。

在我的代码中,我从这个远程站点获取HttpResponse。如果Content-Encoding标头等于,例如“gzip”,我需要将Content-Encoding标头设置为“deflate”。

但是HttpResponse类中没有属性或方法来获取Content-Encoding标头。

在我的例子中,

Content-Encoding属性返回“UTF-8”。在Watch窗口中,我看到_customProperties字段,其中包含错误的字符串值。如何使用 Asp.NET 1.1 更改标题值?

1 个答案:

答案 0 :(得分:0)

无法在Asp.NET 1.1中更改自定义标头。

我只使用反射来解决问题。

// first of all we need get type ArrayList with custom headers:
Type responseType = Response.GetType();
ArrayList fieldCustomHeaders = ArrayList)responseType.InvokeMember("_customHeaders",BindingFlags.GetField|BindingFlags.Instance|BindingFlags.NonPublic, null, Response,null);

// next we go thru all elements of list and search our header
for(int i=0; i < fieldCustomHeaders.Count; i++)
{
    // see all headers
    PropertyInfo propHeaderName = fieldCustomHeaders[i].GetType().GetProperty("Name", BindingFlags.Instance|BindingFlags.NonPublic); 

    String headerName = (String)propHeaderName.GetValue(fieldCustomHeaders[i], null);

    // if we find needed header
    if(headerName == "Content-Encoding")
    {
        // get value of header from its field
        FieldInfo fieldHeaderValue = _fieldCustomHeaders[i].GetType().GetField("_value", BindingFlags.Instance|BindingFlags.NonPublic); 

        String headerValue = (String)fieldHeaderValue.GetValue(fieldCustomHeaders[i]);

        // if we find needed value 
        if (headerValue == "gzip")
        {
            // just set new value to it
            fieldHeaderValue.SetValue(_fieldCustomHeaders[i], "deflate");   
        break;
        }
     }                  
}