0

我有一部分 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更改标头值?

4

1 回答 1

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;
        }
     }                  
}
于 2010-04-13T10:03:42.783 回答