gpt4 book ai didi

node.js - nodejs http响应编码

转载 作者:搜寻专家 更新时间:2023-10-31 23:28:49 24 4
gpt4 key购买 nike

是否可以阅读非utf8编码的网页?例如 windows-1251。我尝试使用 node-iconv 转换结果:

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)

谢谢!

最佳答案

这是您问题的有效解决方案。您必须先使用 Buffer 并将字符串转换为二进制。

request({ 
uri: website_url,
method: 'GET',
encoding: 'binary'
}, function (error, response, body) {
body = new Buffer(body, 'binary');
conv = new iconv.Iconv('windows-1251', 'utf8');
body = conv.convert(body).toString();
}
});

关于node.js - nodejs http响应编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5135450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com