开发者

Asp.NET 1.1 HttpResponse headers

开发者 https://www.devze.com 2022-12-26 07:52 出处:网络
i have part of Asp.NET 1.1 project. I work with remote site, which works incorrect in some cases - sometimes it write incorrect Content-Encoding header.

i have part of Asp.NET 1.1 project.

I work with remote site, which works incorrect in some cases - sometimes it write incorrect Content-Encoding header.

In my code i get HttpResponse from this remote site. And if Content-Encoding header is equals, for example, "gzip", i need to set Content-Encoding header to "deflate".

But there is no properties开发者_Python百科 or methods in HttpResponse class to get Content-Encoding header.

Content-Encoding property returns, in my case, "UTF-8". In Watch window i see _customProperties field, which contain wrong string value. How can i change header value with Asp.NET 1.1?


There is no way to change custom headers in Asp.NET 1.1.

I solve problem only using reflection.

// 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;
        }
     }                  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号