【发布时间】:2023-03-04 22:17:01
【问题描述】:
我有类似以下内容:
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+ 是如何做到的?
【问题讨论】:
-
您的
NameValuePair只是添加了一个请求参数,您没有在代码中发送任何 JSON。服务期望接收什么 JSON 结构,您要发送什么数据?您正在寻找带有StringRequestEntity的postMethod.setRequestEntity(),其中包含您的 JSON。
标签: java json http post apache-commons-httpclient