NodeJS http响应编码
是否可以读取非 utf8 编码的网页?例如 windows-1251。 我尝试使用节点图标转换结果:
var convertedBody = new Iconv('windows-1251','utf-8').convert(responseBody));
但我得到异常:
Error: EILSEQ, Illegal character sequence.
at IncomingMessage.<anonymous> (/root/nodejstest/test2.js:22:19)
at IncomingMessage.emit (events.js:59:20)
at HTTPParser.onMessageComplete (http.js:111:23)
at Socket.ondata (http.js:1183:22)
at Socket._onReadable (net.js:654:27)
at IOWatcher.onReadable [as callback] (net.js:156:10)
谢谢!
Is in possible to read web page in non utf8 encoding? For example windows-1251.
I tried to convert result using node-iconv:
var convertedBody = new Iconv('windows-1251','utf-8').convert(responseBody));
But I get exception:
Error: EILSEQ, Illegal character sequence.
at IncomingMessage.<anonymous> (/root/nodejstest/test2.js:22:19)
at IncomingMessage.emit (events.js:59:20)
at HTTPParser.onMessageComplete (http.js:111:23)
at Socket.ondata (http.js:1183:22)
at Socket._onReadable (net.js:654:27)
at IOWatcher.onReadable [as callback] (net.js:156:10)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是您问题的有效解决方案。您必须首先使用 Buffer 并将字符串转换为二进制。
Here is working solution to your problem. You have to use Buffer and convert your string to binary first.
查看 iconv-lite 库。
使用它你的代码可能如下所示:
Take a look at the iconv-lite library.
Using it your code may look like this:
Iconv 没有
windows-1251
。您可以从 bnoordhuis/node-iconv 验证编码列表。
顺便说一句,来自维基百科:
Iconv doesn't has
windows-1251
.You can verify the list of encodings from bnoordhuis/node-iconv.
BTW, from wikipedia: