【发布时间】:2016-09-05 09:11:51
【问题描述】:
我正在尝试使用正文进行 HTTP 发布请求。我想知道如何将 json 对象放入请求正文中,然后读取正文作为响应。
HttpClient httpClient = HttpClientBuilder.create().build();
String responseString = null;
try {
HttpPost request = new HttpPost(link);
log.info("Executing request " + request.getRequestLine());
StringEntity params = new StringEntity(data);
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
responseString = EntityUtils.toString(entity, "UTF-8");
} catch (Exception e) {
log.info("Exception thrown while executing HTTPPostRequest");
}
return responseString;
这是我目前用来发出请求的代码片段。如果使用“StringEntity”是将数据放入正文中的正确选择。 我得到了空的正文,它应该返回包含 json 数据的正文
【问题讨论】: