Aaron
Aaron

Reputation: 71

HttpResponseMessage Content string appears to be wrong encoding

I'm using a console application to communicate with a third party website. In order to determine if a form submission completed successfully I would like to query the response message to determine if the submit button is still on the page.

The response headers in Fiddler look like this:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 08 May 2015 19:31:34 GMT
Content-Length: 93993

My code looks like this:

var enc = Encoding.UTF8;
using (Stream responseStream =  responseMessage.Content.ReadAsStreamAsync().Result)
            {
                using (var rd = new StreamReader(responseStream, enc))
                {
                    var stringContent = rd.ReadToEnd();
                    if (!stringContent.Contains("submitButtonTag"))
                        result = true;

                }
            }

However when I view the returned string in Visual Studio the characters look like this:

�\b\0\0\0\0\0\0�\a`I�%&/m�{J�J��t�\b�`$ؐ@������iG#)�*��eVe]f@�흼��{���

I would just run through all the way to the submit page to see what happens but I have a limited number of test forms I can submit. Is this just the way that Visual Studio renders these? Is there a way I can get this response to be readable characters that I can compare against?

Upvotes: 1

Views: 2166

Answers (1)

Aaron
Aaron

Reputation: 71

After the comment from @JoeEnos I was able to simply add a GZip decompression before reading the stream. Code is below.

var enc = Encoding.UTF8;

using (Stream responseStream =  responseMessage.Content.ReadAsStreamAsync().Result)
{
    using (var decompressedStream = new GZipStream(responseStream, CompressionMode.Decompress))
    {
        using (var rd = new StreamReader(decompressedStream, enc))
        {
            var stringContent = rd.ReadToEnd();
            if (!stringContent.Contains("submitButtonTag"))
                result = true;

        }
    }                   
}

Upvotes: 3

Related Questions