Java UTF-8 编码不起作用 HttpURLConnection
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18759199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java UTF-8 encoding not working HttpURLConnection
提问by user1365697
I tried to do post call and to pass input with this value - "?愛??????" I got error message
我尝试进行后期调用并使用此值传递输入 - “?爱??????” 我收到错误信息
{"error":{"code":"","message":{"lang":"en-US","value":{"type":"ODataInputError","message":"Bad Input: Invalid JSON format"}}}}
This is my code
这是我的代码
conn.setRequestMethod(ConnectionMethod.POST.toString());
conn.setRequestProperty(CONTENT_LENGTH, Integer.toString(content.getBytes().length));
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(content);
wr.flush();
wr.close();
InputStream resultContentIS;
String resultContent;
try {
resultContentIS = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(resultContentIS));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
it falied on conn.getInputStream();
它在 conn.getInputStream() 上失败了;
The value of content is
内容的价值在于
{ "input" : "?愛??????" }
It is working where the input is String or integer
它在输入是字符串或整数的地方工作
When I added the statement
当我添加声明时
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
I got different message
我收到了不同的消息
{"error":{"code":"","message":{"lang":"en-US","value":{"type":"Error","message":"Internal server error"}}}}
采纳答案by Ha Nguyen
Please try this code below:
请尝试以下代码:
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
writer.write(content);
writer.close();
wr.close();
You should use JSONObject to pass params
您应该使用 JSONObject 来传递参数
The input, please try
输入法,请试试
BufferedReader reader = new BufferedReader(new InputStreamReader(resultContentIS, "UTF-8"));
If the out put is: ???????, so do not worry because your output console do not support UTF-8
如果输出是:???????,所以不要担心,因为您的输出控制台不支持 UTF-8
回答by roehrijn
It seems that your variable contentdoes already have the wrong data because you may have converted a String without any attention to the required encoding.
您的变量内容似乎已经包含错误的数据,因为您可能在没有注意所需编码的情况下转换了字符串。
Setting the correct enconding on the writer and use write()
instead of writeBytes()
should be worth a try.
为作者设置正确的编码并使用write()
而不是writeBytes()
应该值得一试。
回答by Sinan Ergin
You have to send content via byte array
您必须通过字节数组发送内容
DataOutputStream outputStream= new DataOutputStream(conn.getOutputStream());
outputStream.write(content.toString().getBytes());
This is completely solution for your file name character problems. The imported point is string sending via byte array. Every character changing via byte character. This is prevent your character encoding problems.
这是您的文件名字符问题的完全解决方案。导入点是通过字节数组发送的字符串。每个字符都通过字节字符改变。这是防止您的字符编码问题。