Web服务器和UTF-8编码
我正在用 java 编写一个 Web 服务器,当浏览器发送带有重音字符(例如 à è ì ò ù)的字符串(使用 GET 或 POST)时,服务器会收到奇怪的符号。 我想我应该使用UTF-8编码,但我不知道如何。 你能给我建议一个解决方案吗? (客户端/服务器端都可以) 谢谢您的建议!
[编辑] 我在网络服务器上使用InputStreamReader。我做了一个测试,发送“àèìòù”并用wireshark嗅探http数据包:
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n
... 文本=%E8%E0%EC%F2%F9
I'm writing a web server in java, and when the browser sends a string (with GET or POST) with accented characters (for example à è ì ò ù) the server receives strange symbols.
I think I should use UTF-8 encoding, but I don't know how.
Can you suggest to me a solution? (it's ok both client/server side)
Thank you in advice!
[EDIT]
I'm using InputStreamReader on the web server. I did a test sending "àèìòù" and sniffing the http packet with wireshark:
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n
...text=%E8%E0%EC%F2%F9
您需要使用 < 从套接字读取输入流code>InputStreamReader 是用
"UTF-8"
作为第二个参数构造的。但是,如果客户端在Content-Type
请求标头中指定了charset
,那么您应该使用它。You need to read the input stream from the socket using an
InputStreamReader
which is constructed with"UTF-8"
as 2nd argument. But if the client specified acharset
in theContent-Type
request header, then you should use that instead.