无法通过Post请求读取Json utf8

时间:2017-05-21 08:53:09

标签: java post encoding utf-8 request

我知道很多主题是针对Utf8编码问题而制作的。我已经对它进行了探索,但我没有找到可行的解决方案...我有一个函数可以发布帖子请求:

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("Content-Type","application/json;charset=utf-8");它无法读取Json:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Invalid UTF-8 middle byte 0x22

0 个答案:

没有答案