How to unzip client page on HTML pages

I have this proxy code taken from this article and created as 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); } } 

Ive mapped this Http module in IIS7 as a managed hanlder and on my simple Html Im page, using jQuery to call a proxy server and post the results in an 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); } }); }); 

Everything works fine when the page is pleasant and simple, however, if the page is compressed (gzip, deflate) I need to find a way to unpack it on the client side , and not in the proxy server - the proxy function should be as fast as possible.

+4
source share
1 answer

Browsers automatically decompress based on HTTP headers.

I suspect what happens when WebClient automatically decompresses the response received from the upstream server. Your code then passes the decompressed information to the client, but tells the client that the data is compressed.

You cannot directly tell WebClient not to unpack. You must create a derived WebClient class and override the GetWebRequest method. In this method, you can not unpack HttpWebRequest . It will look like this:

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

Then, instead of WebClient use MyWebClient :

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

Source: https://habr.com/ru/post/1333050/


All Articles