简体   繁体   English

HttpResponseMessage内容字符串似乎编码错误

[英]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: Fiddler中的响应标头如下所示:

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: 但是,当我在Visual Studio中查看返回的字符串时,字符如下所示:

�\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? 这只是Visual Studio呈现这些内容的方式吗? Is there a way I can get this response to be readable characters that I can compare against? 有没有一种方法可以使我将我的响应与可读的字符进行比较?

After the comment from @JoeEnos I was able to simply add a GZip decompression before reading the stream. 在@JoeEnos发表评论之后,我能够在读取流之前简单地添加一个GZip解压缩。 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;

        }
    }                   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM