Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 400错误请求[内容长度:0,分块:false]_Java_Json_Rest_Http Post_Special Characters - Fatal编程技术网

Java 400错误请求[内容长度:0,分块:false]

Java 400错误请求[内容长度:0,分块:false],java,json,rest,http-post,special-characters,Java,Json,Rest,Http Post,Special Characters,我正在尝试调用一个api。每当我在请求正文中包含“é”时,就会收到400个错误请求。当我记起时,一切都很好。下面是我的代码和回复 CloseableHttpClient httpClient = (CloseableHttpClient) getHttpClient(); HttpPost httpPost = getHttpPost("xxx"); StringEntity entity = new StringEntity(request.to

我正在尝试调用一个api。每当我在请求正文中包含“é”时,就会收到400个错误请求。当我记起时,一切都很好。下面是我的代码和回复

CloseableHttpClient httpClient = (CloseableHttpClient) getHttpClient();
            HttpPost httpPost = getHttpPost("xxx");
            StringEntity entity = new StringEntity(request.toString());
            entity.setChunked(false);
            httpPost.addHeader("content-type", "application/json");
            httpPost.setEntity(entity);
            System.out.println("contentlength = "+String.valueOf(entity.getContentLength()));
            CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(httpPost);
           System.out.println("response = "
                + response);

            StatusLine statusLine = response.getStatusLine();
           System.out.println("statusLine = "
                + statusLine);

            String responseEntity = EntityUtils.toString(response.getEntity());
           System.out.println("responseEntity = "
                + responseEntity);
答复:

contentlength=964 response=HttpResponseProxy{HTTP/1.1 400错误 请求[Content-Length:0,Chunked:false]}statusLine=HTTP/1.1 400 请求响应错误=


将内容类型设置为:

"application/json;charset=UTF-8" 
在您使用的应用程序中发送post请求时

StringEntity entity = new StringEntity(request.toString(),"UTF-8");

这解决了问题。

尝试过。但是没有运气。