如何解压缩客户端的Html页面

时间:2010-12-22 15:11:01

标签: c# asp.net jquery

我从此article获取此代理代码并创建为HttpHandler

public void ProcessRequest(HttpContext context)
{
        string url = context.Request["url"];
        string contentType = context.Request["type"];

        // no buffering as we want to save memory
        context.Response.Buffer = false;

        // beging getting content
        using (WebClient client = new WebClient())
        { 

            // set content type if specified
            if (!string.IsNullOrEmpty(contentType))
            {
                client.Headers.Add(HttpRequestHeader.ContentType, contentType);
            }

            client.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            client.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US");
            client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows; U; Windows NT 6.0; " +
               "en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");                
            client.Headers.Add(HttpRequestHeader.Accept, "*/*");

            // get that data
            byte[] data = client.DownloadData(url);

            if (!context.Response.IsClientConnected) return;

            // deliver content type, encoding and length as it
            // is received from the external url
            context.Response.ContentType = client.ResponseHeaders["Content-Type"];

            string contentEncoding = client.ResponseHeaders["Content-Encoding"];
            string contentLength = client.ResponseHeaders["Content-Length"];

            if (!string.IsNullOrEmpty(contentEncoding))
                context.Response.AppendHeader(HttpRequestHeader.ContentEncoding.ToString(), contentEncoding);

            if (!string.IsNullOrEmpty(contentLength))
                context.Response.AppendHeader(HttpRequestHeader.ContentLength.ToString(), contentLength);


            // transmit the exact bytes downloaded
            context.Response.BinaryWrite(data);


    }
}

我已经将IIS7中的这个Http模块映射为托管hanlder,在我的普通Html页面中,我使用jQuery调用代理并将结果放在iframe中。

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "a.RegularProxy",
        data: { url: 'http://example.org/test.html', type: "text/html" },
        dataType: "html",
        success: function(data) {
            $("iframe").contents().find('html body').html(data.toString());
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
});

当页面很简单时,一切正常,但是如果页面被压缩(gzip,deflate)我需要找到一种方法在客户端解压缩而不是代理内部 - 代理的功能应尽可能快。

1 个答案:

答案 0 :(得分:3)

浏览器会根据HTTP标头自动进行解压缩。

我怀疑正在发生的事情是WebClient会自动解压缩从上游服务器收到的响应。然后,您的代码将解压缩的信息传输到客户端,但告诉客户端数据已压缩。

直接告诉WebClient不要解压缩是没有办法的。您必须创建派生的WebClient类并覆盖GetWebRequest方法。在该方法中,您告诉HttpWebRequest不要解压缩。它看起来像这样:

public class MyWebClient: WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest wr = base.GetWebRequest(address);
        wr.AutomaticDecompression = DecompressionMethods.None;
        return wr;
    }
}

然后,您使用MyWebClient代替WebClient

using (MyWebClient client = new MyWebClient)
{
    // do your thing here
}