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

问题描述 投票:0回答:2

我想打电话给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 Bad Request [Content-Length:0,Chunked:false]} statusLine = HTTP / 1.1 400 Bad Request responseEntity =

java json rest http-post special-characters
2个回答
0
投票

将content-type设置为:

"application/json;charset=UTF-8" 

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


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

这解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.