您的位置:首页 > 理论基础 > 计算机网络

httpclient post 发送Json数据

2015-12-28 00:00 633 查看
摘要: 包:httpclient-4.5.1 httpcore-4.4.3

String url = "http://127.0.0.1:8099/login";
String json = "{\"login_name\": \"18800000000\",\"login_password\": \"123456\"}";
@Test
public void post() throws ClientProtocolException, IOException{
//创建client
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//创建post请求
HttpPost httppost = new HttpPost(url);
//json
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httppost.setEntity(entity);
System.out.println("executing request " + httppost.getRequestLine());
//执行post请求
CloseableHttpResponse response = httpclient.execute(httppost);
try {
System.out.println("----------------------------------------");
//状态
System.out.println(response.getStatusLine());
//响应实体
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
//关闭HttpEntity流
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  httpclient post Json