HttpWebResponse - 编码问题
我的编码有问题。当我获得网站的源代码时,我有:
我将编码设置为 UTF8,如下所示:
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string sourceCode = reader.ReadToEnd();
感谢您的帮助!
I have a problem with encoding. When I get site's source code I have:
I set encoding to UTF8 like this:
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string sourceCode = reader.ReadToEnd();
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用指定的编码:
如果您以某种方式接受 gzip,这可能会有所帮助:(我自己没有尝试过,并且承认它没有多大意义,因为您的编码不是 gzip?!)
Try to use the encoding specified:
If you are accepting gzip somehow, this may help: (Haven't tried it myself and admittedly it doesn't make much sense since your encoding is not gzip?!)
我遇到了同样的问题,我尝试更改编码,从源到结果,但我什么也没得到。最后,我遇到了一条线索,引导我进入以下内容......
看看这里...
.NET:是否可以获取HttpWebRequest 自动解压缩 gzip 响应?
在从请求中检索响应之前,您需要使用以下代码。
因为一旦我们使用接受编码“gzip”或“deflate”,数据就会被压缩,并变成我们无法读取的数据。所以我们需要解压它们。
I had the same issue, I tried changing encoding, from the source to the result, and I got nothing. in the end, I come across a thread that leads me to the following...
Take look here...
.NET: Is it possible to get HttpWebRequest to automatically decompress gzip'd responses?
you need to use the following code, before retrieving the response from the request.
since once we use accept-encoding 'gzip' or 'deflate', the data get compressed, and turn into data unreadable by us. so we need to decompress them.
但响应可能不是 UTF-8。您是否检查了响应对象的
CharacterSet
和ContentType
属性以确保您使用的是正确的编码?无论如何,这两个字符看起来就像值 03 和 08 的代码页 437 字符。看起来数据流中存在一些二进制数据。
我建议为了调试,您使用 Stream.Read 将响应中的前几个字节读取到字节数组中,然后检查这些值以查看您得到的内容。
But the response might not be UTF-8. Have you checked the
CharacterSet
and theContentType
properties of the response object to make sure you're using the right encoding?In any event, those two characters look like the code page 437 characters for values 03 and 08. It looks like there's some binary data in your data stream.
I would suggest that for debugging, you use
Stream.Read
to read the first few bytes from the response into a byte array and then examine the values to see what you're getting.更改代码中的这一行:
它可能会帮助你..
Change this line in your code:
it may help you..