Java HttpPost在请求主体中发布复杂的JSONObject
我想知道,使用HttpClient和HttpPOST是否有一种方法可以将一个复杂的JSON对象发布为请求体?我确实看到了在正文中发布一个简单的键/值对的示例(如下链接所示:): 我有办法做到这一点吗 其他信息 执行以下操作:Java HttpPost在请求主体中发布复杂的JSONObject,java,httpclient,apache-httpclient-4.x,apache-commons-httpclient,Java,Httpclient,Apache Httpclient 4.x,Apache Commons Httpclient,我想知道,使用HttpClient和HttpPOST是否有一种方法可以将一个复杂的JSON对象发布为请求体?我确实看到了在正文中发布一个简单的键/值对的示例(如下链接所示:): 我有办法做到这一点吗 其他信息 执行以下操作: HttpClient client= new DefaultHttpClient(); HttpPost request = new HttpPost("www.example.com"); String json = "{\"value\": {\"id\": \"123
HttpClient client= new DefaultHttpClient();
HttpPost request = new HttpPost("www.example.com");
String json = "{\"value\": {\"id\": \"12345\",\"type\": \"weird\"}}";
StringEntity entity = new StringEntity(json);
request.setEntity(entity);
request.setHeader("Content-type", "application/json");
HttpResponse resp = client.execute(request);
导致服务器上出现空正文。。。因此我得到了400分
提前谢谢 HttpPost.setEntity()
接受扩展。您可以使用您选择的任何有效字符串设置它:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost request = new HttpPost("www.example.com");
String json = "{\"value\": {\"id\": \"12345\",\"type\": \"weird\"}}";
StringEntity entity = new StringEntity(json);
entity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(entity);
request.setHeader("Content-type", "application/json");
HttpResponse resp = client.execute(request);
HttpPost.setEntity()
接受扩展名。您可以使用您选择的任何有效字符串设置它:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost request = new HttpPost("www.example.com");
String json = "{\"value\": {\"id\": \"12345\",\"type\": \"weird\"}}";
StringEntity entity = new StringEntity(json);
entity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(entity);
request.setHeader("Content-type", "application/json");
HttpResponse resp = client.execute(request);
这对我有用
HttpClient client= new DefaultHttpClient();
HttpPost request = new HttpPost("www.example.com");
String json = "{\"value\": {\"id\": \"12345\",\"type\": \"weird\"}}";
StringEntity entity = new StringEntity(json);
entity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(entity);
request.setHeader("Content-type", "application/json");
HttpResponse resp = client.execute(request);
这对我有用
HttpClient client= new DefaultHttpClient();
HttpPost request = new HttpPost("www.example.com");
String json = "{\"value\": {\"id\": \"12345\",\"type\": \"weird\"}}";
StringEntity entity = new StringEntity(json);
entity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
request.setEntity(entity);
request.setHeader("Content-type", "application/json");
HttpResponse resp = client.execute(request);
出于某种原因,这给了我一个400,我可以通过邮递员得到这个工作请求…:(还有其他想法吗?当我检查服务器作为主体得到的是什么时,我这样做时什么也得不到。但是,当我按照我在问题中发布的方式进行操作并将垃圾值添加到BasicNameValuePair时,我可以看到服务器出于某种原因将这些值设置为主体,这会返回400,我可以通过postm获得此请求安…:(还有其他想法吗?当我检查服务器作为主体得到的是什么时,我这样做什么也得不到。但是,当我按照我在问题中发布的方式来做,并将垃圾值添加到BasicNameValuePair中时,我可以看到服务器将这些值设置为主体,我很高兴听到这个消息!如果你觉得我的答案对你有帮助,请放心e接受答案:)我很高兴听到这个消息!如果你觉得我的答案对你有帮助,请随意接受答案:)试试这个,StringEntity requestBody=new StringEntity(,ContentType.APPLICATION\u JSON)
试试这个,StringEntity requestBody=new StringEntity(,ContentType.APPLICATION\u JSON)