0

有时我会从几个网站得到一种乱码的响应。

这是我的代码:

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 请求时,我在相应的字符串中得到以下字符:

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

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

4

2 回答 2

6

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 Thanks for your help!!!

于 2011-09-28T08:27:01.773 回答
3

请参阅如何在 C# 中使用 GetResponseStream 方法以及HttpWebResponse 和 HttpWebRequest的用法,了解如何从 HttpWebResponse 读取内容。希望它会帮助你。

于 2011-09-27T09:37:29.520 回答