Java Can';使用Post请求读取Json utf8
我知道很多主题都是针对Utf8编码问题设计的。我已经研究过了,但没有找到有效的解决方案。。。我得到了一个函数来发出Post请求:Java Can';使用Post请求读取Json utf8,java,post,encoding,utf-8,request,Java,Post,Encoding,Utf 8,Request,我知道很多主题都是针对Utf8编码问题设计的。我已经研究过了,但没有找到有效的解决方案。。。我得到了一个函数来发出Post请求: private void sendingPostRequest(String url, String postJsonData) throws Exception { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection();
private void sendingPostRequest(String url, String postJsonData) throws Exception {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// Setting basic post request
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type","application/json;charset=utf-8");
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(postJsonData);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String output;
StringBuffer response = new StringBuffer();
while ((output = in.readLine()) != null) {
response.append(output);
}
in.close();
//printing result from response
System.out.println(response.toString());
}
下面是一个postJsonData
的示例:
"{"t":"ñ"}"
尽管有con.setRequestProperty(“内容类型”,“应用程序/json;字符集=utf-8”)代码>它无法读取Json:
Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Invalid UTF-8 middle byte 0x22