如何使用Apache HttpClient发布JSON请求? [英] How to POST JSON request using Apache HttpClient?
问题描述
我有以下内容:
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 +?
Apache HttpClient不会我对JSON一无所知,所以你需要单独构建你的JSON。为此,我建议你查看简单的 json.org 的noreferrer> JSON-java 库。(如果JSON-java没有不适合你,json.org有很多不同语言的库。)
一旦你生成了你的JSON,就可以使用代码之类的东西了。在下面发布它
StringReques tEntity 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);
I have something like the following:
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();
It keeps coming back with a 500. The service provider says I need to send JSON. How is that done with Apache HttpClient 3.1+?
Apache HttpClient doesn't know anything about JSON, so you'll need to construct your JSON separately. To do so, I recommend checking out the simple JSON-java library from json.org. (If "JSON-java" doesn't suit you, json.org has a big list of libraries available in different languages.)
Once you've generated your JSON, you can use something like the code below to POST it
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);
Edit
Note - The above answer, as asked for in the question, applies to Apache HttpClient 3.1. However, to help anyone looking for an implementation against the latest Apache client:
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);
这篇关于如何使用Apache HttpClient发布JSON请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!