Does .Net's Httpwebresponse Uncompress Automatically Gziped and Deflated Responses

Does .NET's HttpWebResponse uncompress automatically GZiped and Deflated responses?

I found the answer.

You can change the code to:

var request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

And you will have automatic decompression. No need to change the rest of the code.

How Decompress Gzipped Http Get Response in c#

Just Change My Function as Follows,Which is Perfectly Working For me:

private JObject PostingToPKFAndDecompress(string sData, string sUrl)
{
var jOBj = new JObject();
try
{

try
{
string urlStr = @"" + sUrl + "?param=" + sData;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlStr);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();

var t = ReadFully(resStream);
var y = Decompress(t);

using (var ms = new MemoryStream(y))
using (var streamReader = new StreamReader(ms))
using (var jsonReader = new JsonTextReader(streamReader))
{
jOBj = (JObject)JToken.ReadFrom(jsonReader);
}

}
catch (System.Net.Sockets.SocketException)
{
// The remote site is currently down. Try again next time.
}

}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
return jOBj;
}

public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}

public static byte[] Decompress(byte[] data)
{
using (var compressedStream = new MemoryStream(data))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}

Uncompressing gzip response from WebClient

The easiest way to do this is to use the built in automatic decompression with the HttpWebRequest class.

var request = (HttpWebRequest)HttpWebRequest.Create("http://stackoverflow.com");
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

To do this with a WebClient you have to make your own class derived from WebClient and override the GetWebRequest() method.

public class GZipWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
return request;
}
}

Also see this SO thread: Does .NET's HttpWebResponse uncompress automatically GZiped and Deflated responses?

How do I decode an encoded HttpWebResponse?

The answer is in the response header: Content-Encoding: br -> This means Brotli compression.

There is a .NET implementation (NuGet package) for it:

Install this to your project add "using Brotli; " and replace the "using (StreamReader....." with this code:

       using (BrotliStream bs = new BrotliStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress)) {
using (System.IO.MemoryStream msOutput = new System.IO.MemoryStream()) {
bs.CopyTo(msOutput);
msOutput.Seek(0, System.IO.SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(msOutput)) {
html = reader.ReadToEnd();
}
}
}

Speed of HttpWebRequest/HttpWebResponse

(You should have a using statement for the response, by the way... and I agree with asbjornu's comment. You should update your question with more details.)

You should use something like Wireshark to look at what the requests and responses look like in each case. For example, is the browser specifying that it supports compressed responses, and WebRequest not? If it's over a slow connection, that could well be the important part.

Another thing to test is whether the string decoding is taking significant time in the .NET code... if you simply read the data from the stream into a byte array (possibly just throwing it away as you read it) is that significantly faster? For example:

using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
// Just read the data and throw it away
byte[] buffer = new byte[16 * 1024];
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
// Ignore the actual data
}
}
}


Related Topics



Leave a reply



Submit