在node.js http客户端中自动进行utf-8编码 [英] automatic utf-8 encodeing in node.js http client

查看:375
本文介绍了在node.js http客户端中自动进行utf-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从使用node.js的远程主机加载XML。
问题是德国umlaute如ä被打破了。像浏览器一样,这个通常是一个简单的编码问题。但是,由于远程主机上的XML编码为iso-8859-2我没有成功使信件恢复工作。



功能非常简单b $ b我简单地使用node.js中集成的默认http客户端,通过简单的get请求连接到远程主机。



有些env事实:
远程系统使用iso-8859-2编码
编码在响应头中被正确设置
由response.onData(chunk)接收的数据(块)中的字符是不可恢复的,



Node.js在默认debian服务器上的版本0.2上运行。



代码基于我尝试以下操作:
response.defaultAsciiEncoding true / false
response.encoding = UFT -8 / ascii



使用utf8编码器/解码器对该块
进行编码/解码,此失败后我尝试对整个响应体进行编码/解码



我不太熟悉使用缓冲区,我想这个问题必须在这个方向。或者node.js(或httpClient)根本无法处理其他enc类型,这是我的第二个猜测。在这种情况下,我需要编写我自己的http客户端使用网络我想。我只是想确保我不走错误的方向:)



THX帮助甚至在猜测时gl& hf;)!

解决方案

我对node.js源进行了一个快速的敲击,似乎svick是正确的:node.js不支持iso编码。然而,您可以将响应作为二进制流,然后将其返回到具有自己编码的浏览器或使用node-iconv(再次以svick建议)。



以下是一个例子: http://gist.github.com/576884


Hi there i am trying to load an XML from a remote host useing node.js. The problem is that german "umlaute" like "ä" are broken. Like in the browser this usualy is a simple encoding problem. But since the XML on the remote host is encoded in iso-8859-2" i had no success getting the letters back to work.

The functionality is very simple. I simply use the default http client integrated in node.js to connect to a remote host with a simple get request.

Some env facts: The remote system uses "iso-8859-2" encoding. The encoding is currectly set in the response header. The chars are unrecoverable broken in the data (chunk) received by response.onData(chunk)

Node.js is running on Version 0.2 on da default debian server.

The code is based on the default httpClient like descriped in the node.js documentation.

I tried the following: response.defaultAsciiEncoding true/false response.encoding = UFT-8/ascii

used a utf8 encoder/decoder to encode/decode the chunk after this failed i tried to encode/decode the whole response body

I am not very familiar with useing buffers and i guess the problem must be in that direction. Or node.js(or the httpClient) simply cant handle other enc types by default witch is my second guess. In this case i need to write my own http client useing the net lib i think. I just want to make sure i don't walk into the wrong direction :)

THX for helping or even gl&hf while guessing ;)!

解决方案

I had a quick poke around the node.js source and it seems like svick is right: node.js doesn't support the iso encoding. You can, however, get at the response as a binary stream and then either return it to the browser with your own encoding or use node-iconv (again as svick suggested).

Here's a little example: http://gist.github.com/576884

这篇关于在node.js http客户端中自动进行utf-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆