views:

183

answers:

1

Hi, 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 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?

A: 

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;
        }
     }                  
}
Yeti