400错误的请求[Content-Length:0,Chunked:false]

时间:2019-02-25 17:36:53

标签: java json rest http-post special-characters

我正在尝试调用一个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响应= HttpResponseProxy {HTTP / 1.1 400错误   请求[Content-Length:0,Chunked:false]} statusLine = HTTP / 1.1 400   错误的请求responseEntity =

2 个答案:

答案 0 :(得分:0)

将内容类型设置为:

"application/json;charset=UTF-8" 

在您正在使用的应用程序中发送发布请求时。

答案 1 :(得分:0)

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

这解决了问题。