如何以正确的字符串形式获取httpWebresponse的内容? [英] How to get content of httpWebresponse in proper string form?

查看:396
本文介绍了如何以正确的字符串形式获取httpWebresponse的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我从几个网站得到一些乱码的回应。

Sometimes I am getting kind of garbled response from several web sites.

这是我的代码:

Stream responseStream = response.GetResponseStream();
buffer = new Byte[256];//
int bytesRead;
while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
   outStream.Write(buffer, 0, bytesRead);
   //resp=resp+ .UTF8.GetString(buffer, 0, bytesRead);
   resp=resp + Encoding.ASCII.GetString(buffer); //resp is string
}

当我向www.google.co.in我得到以下字符在resp字符串:

when I request from www.google.co.in I get following characters in resp string:


?\b\0\0\0\0\0? z} y→F······················································································································································································{

?\b\0\0\0\0\0??}y?F?????????Z??????{7m???oX?\r?Y???33??d;y????n?0?

我应该如何解决这个问题?它与编码有关吗?

How should I overcome this problem? Is it related to encoding?

推荐答案

我收到的响应是GZip压缩,所以我只是解压缩响应流如下所示:

The response I received was GZip-compressed, so I just decompressed the response stream as shown below:

Stream responseStream = response.GetResponseStream();
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);

现在可以使用上面提供的代码读取流。

now one can read the stream using the code I provided above.

@Kalyan感谢您的帮助!

@Kalyan Thanks for your help!!!

这篇关于如何以正确的字符串形式获取httpWebresponse的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆