103

我有以下内容:

final String url = "http://example.com";

final HttpClient httpClient = new HttpClient();
final PostMethod postMethod = new PostMethod(url);
postMethod.addRequestHeader("Content-Type", "application/json");
postMethod.addParameters(new NameValuePair[]{
        new NameValuePair("name", "value)
});
httpClient.executeMethod(httpMethod);
postMethod.getResponseBodyAsStream();
postMethod.releaseConnection();

它不断返回 500。服务提供商说我需要发送 JSON。Apache HttpClient 3.1+ 是如何做到的?

4

4 回答 4

202

Apache HttpClient 对 JSON 一无所知,因此您需要单独构建 JSON。为此,我建议从json.org查看简单的JSON-java库。(如果“JSON-java”不适合你,json.org 有很多不同语言的库。)

一旦你生成了你的 JSON,你可以使用类似下面的代码来发布它

StringRequestEntity requestEntity = new StringRequestEntity(
    JSON_STRING,
    "application/json",
    "UTF-8");

PostMethod postMethod = new PostMethod("http://example.com/action");
postMethod.setRequestEntity(requestEntity);

int statusCode = httpClient.executeMethod(postMethod);

编辑

注意 - 如问题中所要求的,上述答案适用于 Apache HttpClient 3.1。但是,为了帮助任何寻求针对最新 Apache 客户端实现的人:

StringEntity requestEntity = new StringEntity(
    JSON_STRING,
    ContentType.APPLICATION_JSON);

HttpPost postMethod = new HttpPost("http://example.com/action");
postMethod.setEntity(requestEntity);

HttpResponse rawResponse = httpclient.execute(postMethod);
于 2012-08-21T17:36:17.593 回答
28

对于Apache HttpClient 4.5或更新版本:

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("http://targethost/login");
    String JSON_STRING="";
    HttpEntity stringEntity = new StringEntity(JSON_STRING,ContentType.APPLICATION_JSON);
    httpPost.setEntity(stringEntity);
    CloseableHttpResponse response2 = httpclient.execute(httpPost);

笔记:

1 为了使代码编译,httpclient包和httpcore包都应该被导入。

2 try-catch 块已被省略。

参考appache官方指南

Commons HttpClient 项目现已结束,不再开发。它已被其 HttpClient 和 HttpCore 模块中的 Apache HttpComponents 项目所取代

于 2018-10-25T01:39:49.153 回答
10

正如janoside的出色回答中所述,您需要构造 JSON 字符串并将其设置为StringEntity.

要构造 JSON 字符串,您可以使用任何您熟悉的库或方法。Jackson 库就是一个简单的例子:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;

ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.createObjectNode();
node.put("name", "value"); // repeat as needed
String JSON_STRING = node.toString();
postMethod.setEntity(new StringEntity(JSON_STRING, ContentType.APPLICATION_JSON));
于 2018-03-20T01:02:33.137 回答
1

我使用 JACKSON 库将对象转换为 JSON 并设置请求正文,如下所示。这是完整的示例。

try (CloseableHttpClient httpclient = HttpClients.createDefault()) {

    HttpPost httpPost = new HttpPost("https://jsonplaceholder.typicode.com/posts");

    Post post = new Post("foo", "bar", 1);
    ObjectWriter ow = new ObjectMapper().writer();
    String strJson = ow.writeValueAsString(post);
    System.out.println(strJson);

    StringEntity strEntity = new StringEntity(strJson, ContentType.APPLICATION_JSON);
    httpPost.setEntity(strEntity);
    httpPost.setHeader("Content-type", "application/json");

    try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
        System.out.println(response.getCode() + " " + response.getReasonPhrase());

        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity);
        System.out.println(result);

        EntityUtils.consume(entity);
    } catch (ParseException e) {
        e.printStackTrace();
    }

} catch (IOException e) {
    System.out.println(e.getMessage());
}
于 2022-01-29T02:33:36.780 回答