使用 StreamReader 阅读外语网页
我正在尝试获取一个混合英语和韩语的网页。浏览器可以很好地获取并显示页面,但是当我尝试以编程方式获取它时,我无法让韩文字符正确显示。
我知道您可以在 StreamReader 中指定一种编码,但我还没有找到有效的编码。
这是我用来读取响应的代码:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
response.CharacterSet 返回 UTF8。我还尝试了所有基本编码选项 - ASCII、BigEndian、Default、Unicode、UTF32、UTF7 以及手动添加 Encoding.UTF8。
我还尝试通过 CultureInfo 来解决这个问题:
CultureInfo kr = CultureInfo.GetCultureInfo("ko");
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(kr.TextInfo.ANSICodePage));
同时使用“ko”和“ko-KR”。我从所有这些不同类型中得到了不同的结果,但没有一个是正确的。
我还直接尝试了代码页:
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(949));
response.ContentEncoding
返回一个空字符串。我已经没有主意了。
编辑:这是我所期望的示例:
프로젝트:
这是我得到的:
//ASCII == ??????
//BigEndian == ़汩湫â¨ç‰¥æ˜½âˆ¯æ©³â½¤ç°æ”
//Default == Ãâ€â€žÃ«Â¡Å“ì Âʸ:
//Unicode == Ãâ€â€žÃ«Â¡Å“ì Âʸ
//UTF32 == ���������ï
//UTF7 == Ô„ë¡œì Âʸ
//UTF8 == 프로ì 트
I'm trying to fetch a web page that is a mix of English and Korean. The browser can fetch and display the page just fine, but when I try to grab it programmatically I can't get the Korean characters to display properly.
I know that you can specify an Encoding in the StreamReader but I haven't found one that works yet.
This is the code that I'm using to read the response:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
response.CharacterSet returns UTF8. I've also tried all of the basic encoding options - ASCII, BigEndian, Default, Unicode, UTF32, UTF7, and manually adding Encoding.UTF8.
I've also tried going about it through the CultureInfo:
CultureInfo kr = CultureInfo.GetCultureInfo("ko");
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(kr.TextInfo.ANSICodePage));
using both "ko" and "ko-KR". I get varied results from all these different types, but none of them are correct.
I've also tried the code page directly:
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(949));
response.ContentEncoding
returns an empty string. I'm running out of ideas.
Edit: Here is an example of what I'm expecting:
프로젝트:
and here is what I'm getting:
//ASCII == ??????
//BigEndian == ़汩湫â¨ç‰¥æ˜½âˆ¯æ©³â½¤ç°æ”
//Default == Ãâ€â€žÃ«Â¡Å“ì Âʸ:
//Unicode == Ãâ€â€žÃ«Â¡Å“ì Âʸ
//UTF32 == ���������ï
//UTF7 == Ô„ë¡œì Âʸ
//UTF8 == 프로ì 트
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FWIW:流阅读器可能无法正常工作。
首选使用 HttpWebRequest 类 执行浏览器请求(或者当您收到 302 响应或压缩和/或分块编码时您很快就会开始感到抱歉)
我将其提升为答案,因为它可能非常好你已经遇到的问题。当然,我不知道您收到的回复是什么样的
FWIW: a stream reader is likely not going to work well.
Prefer using HttpWebRequest Class to do browser requests (or you will start feeling sorry very soon when you get 302 responses or gzipped and/or chunked encoding)
I promoted this to an answer, as it might very well be the problem you're having already. I don't know what the response you are getting looks like, of course