我想打电话给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 =
将content-type设置为:
"application/json;charset=UTF-8"
在您正在使用的应用程序中发送post请求时。
StringEntity entity = new StringEntity(request.toString(),"UTF-8");
这解决了这个问题。