Uncompress Gzip Compressed Http Response

Uncompress gzip compressed http response

gzuncompress won't work for the gzip encoding. It's the decompression function for the .Z archives.

The manual lists a few workarounds for the missing gzdecode()#82930, or just use the one from upgradephp, or the gzopen temp file workaround.

Another option would be forcing the deflate encoding with the Accept-Encoding: header and then using gzinflate() for decompression.

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();
}
}

Gzip decompression of http Response

I have got a solution as need to add one more key to the options object as :

var options = {
url: url,
qs: params.qparams,
method: params.method,
json: params.body,
headers: {
'api_key': configkey,
'Content-Type': 'application/json',
'Accept-Encoding': 'gzip'
},
timeout: constants.request_timeout,
encoding: null
};

If someone has a better approach to perform the decompression, please add-on.

Decompress a gzip-compressed HTTP-Response (chunked encoding)

Well, I just tried myself and it worked as I described it in my Question. If your interested into my code, just commend or write me a message.

Uncompress GZIPed HTTP Response in Java

You don't show how you get the tBytes that you use to set up the gzip stream here:

GZIPInputStream  gzip = new GZIPInputStream (new ByteArrayInputStream (tBytes));

One explanation is that you are including the entire HTTP response in tBytes. Instead, it should be only the content after the HTTP headers.

Another explanation is that the response is chunked.

edit: You are taking the data after the content-encoding line as the message body. However, according to the HTTP 1.1 specification the header fields do not come in any particular order, so this is very dangerous.

As explained in this part of the HTTP specification, the message body of a request or response doesn't come after a particular header field but after the first empty line:

Request (section 5) and Response
(section 6) messages use the generic
message format of RFC 822 [9] for
transferring entities (the payload of
the message). Both types of message
consist of a start-line, zero or more
header fields (also known as
"headers"), an empty line (i.e., a
line with nothing preceding the CRLF)
indicating the end of the header
fields, and possibly a message-body.

You still haven't show how exactly you compose tBytes, but at this point I think you're erroneously including the empty line in the data that you try to decompress. The message body starts after the CRLF characters of the empty line.

May I suggest that you use the httpclient library instead to extract the message body?

Decompressing GZip Stream from HTTPClient Response

Just instantiate HttpClient like this:

HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};

using (var client = new HttpClient(handler)) //see update below
{
// your code
}

Update June 19, 2020:
It's not recommended to use httpclient in a 'using' block as it might cause port exhaustion.

private static HttpClient client = null;

ContructorMethod()
{
if(client == null)
{
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
client = new HttpClient(handler);
}
// your code
}

If using .Net Core 2.1+, consider using IHttpClientFactory and injecting like this in your startup code.

 var timeout = Policy.TimeoutAsync<HttpResponseMessage>(
TimeSpan.FromSeconds(60));

services.AddHttpClient<XApiClient>().ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
}).AddPolicyHandler(request => timeout);

Uncompress GZIP http-response (using jersey client api, java)

Simply add GZIPContentEncodingFilter to your client:

client.addFilter(new GZIPContentEncodingFilter(false));

Detect gzip encoding to manually decompress response, but 'Content-Encoding' header missing

I gave in to the stubbornness of the uber api and added another request header, req.Header.Add("Accept-Encoding", "gzip").

Now i am getting the response header "Content-Encoding": "gzip", although i am still getting an undecipherable response body, but that's beyond the scope of this question.



Related Topics



Leave a reply



Submit