在Java中使用JSON的http POST我想在Java中使用JSON制作一个简单的HTTPPOST。假设URL是www.site.com它的价值{"name":"myname","age":"20"}标为'details'例如。我将如何创建文章的语法?我似乎也找不到JSON Javadocs中的POST方法。
3 回答
慕娘9325324
TA贡献1783条经验 获得超4个赞
JSON-Java
JSONObject json = new JSONObject();json.put("someKey", "someValue"); CloseableHttpClient httpClient = HttpClientBuilder.create().build();try { HttpPost request = new HttpPost("http://yoururl"); StringEntity params = new StringEntity(json.toString()); request.addHeader("content-type", "application/json"); request.setEntity(params); httpClient.execute(request);// handle response here...} catch (Exception ex) { // handle exception here} finally { httpClient.close();}
添加回答
举报
0/150
提交
取消