Junior
Junior

Reputation: 11990

decompressing a gzip encoded response

I am using C# to write a small console app. The app needs to make a call to an API service to login.

I was able to make the call successfully. But, I am unable to decode the response

Here is my code

using (var client = new WebClient())
{
    client.Headers.Add("User-Agent", "Console App");
    client.Headers.Add("RETS-Version", "RETS/1.7.2");
    client.Headers.Add("Accept-Encoding", "gzip");
    client.Headers.Add("Accept", "*/*");
    client.Credentials = new NetworkCredential("username", "password");

    try
    {
        var response = client.DownloadData("url/login.ashx");

        MemoryStream stream = new MemoryStream(response);

        using (var stram = new GZipStream(stream, CompressionMode.Decompress ))
        using (var file = File.Create("../../../Downloads/login_result.txt"))
        {
            stream.CopyTo(file);
        }

    } catch(Exception e)
    {

    }
}

However, the data that gets written to the login_result.txt file looks something like this

‹      í½`I–%&/mÊ{JõJ×àt¡€`$Ø@ìÁˆÍæ’ìiG#)«*ÊeVe]f@Ìí¼÷Þ{ï½÷Þ{ï½÷º;N'÷ßÿ?\fdlöÎ

How can I correctly decode the response?

Upvotes: 0

Views: 111

Answers (1)

mexanichp
mexanichp

Reputation: 433

Probably you should copy GZip decompressed stream and not the memory one:

using (var stram = new GZipStream(stream, CompressionMode.Decompress ))
using (var file = File.Create("../../../Downloads/login_result.txt"))
{
    stram.CopyTo(file); //stream.CopyTo(file);
}

Upvotes: 1

Related Questions