Node.js:在win1251字符集中发送http请求
如何在win1251字符集中发送以下查询?
var getData = querystring.stringify({ type: "тест", note: "тест1" }), options = { host: config.host, path: config.path + '?' + getData, method: 'GET' }; http.request(options, function (res) {...}).end();
我认为这个片段可以帮助你
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(); } });
更新1
好吧,我想找个有用的东西:)
请检查这个链接
你可以像这样使用上面的工具
// Suppose gbkEncodeURIComponent function already exists, // it can encode string with `gbk` encoding querystring.stringify({ w: '中文', foo: 'bar' }, null, null, { encodeURIComponent: win2unicode }) // returns 'w=%D6%D0%CE%C4&foo=bar'
服务器是否真的接受URL的查询部分中的win1251?
我应该假设一个URL中的编码字符应该在什么字符集?
但是这里有一些符合你的问题的答案:
在Node.js中从Windows-1251转换为UTF-8
nodejs http响应编码
这可以减less使用这些库中的任何一个,你也可以在npm上find这些库。
https://github.com/bnoordhuis/node-iconv
要么
https://github.com/ashtuchkin/iconv-lite
迈克尔