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

标签 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 response = HttpResponseProxy{HTTP/1.1 400 Bad Request [Content-Length: 0,Chunked: false]} statusLine = HTTP/1.1 400 Bad Request responseEntity =

最佳答案

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

这解决了问题。

关于java - 400 错误请求 [Content-Length : 0, Chunked: false],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54871750/

相关文章:

java - Java 的 Netty 和 GUI

php - 如何在使用 JSON_FORCE_OBJECT 参数时删除 php json 编码函数中的 JSON 对象 ID

javascript - Javascript如何使用Fetch API跨域加载文本或JSON数据?

json - 如何快速从 Any 类型访问我的 json 数据?

java - 无法在 apache-camel 中创建路由

ios - Swift 4.2 - 解码相同 key 不同类型的 JSON

java - 如何使用 Guava 将可能的空值转换为默认值?

java - 如何使用java将文件从Windows计算机发送(移动/复制)到服务器

java scala交叉编译相同模块错误java : cannot find symbol

iPhone REST 客户端