我正在尝试调用一个 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/