-
java
-
// 接口测试-处理json格式的post请求
-
public static String doPostJson(String url,String json) {
-
// 创建连接池
-
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
-
ResponseHandler<String> responseHandler = new BasicResponseHandler();
-
// 声明呀一个字符串用来存储response
-
String result;
-
// 创建httppost对象
-
HttpPost httpPost = new HttpPost(url);
-
// 给httppost对象设置json格式的参数
-
StringEntity httpEntity = new StringEntity(json,"utf-8");
-
// 设置请求格式
-
httpPost.setHeader("Content-type","application/json");
-
// 传参
-
httpPost.setEntity(httpEntity);
-
// 发送请求,并获取返回值
-
try {
-
CloseableHttpResponse response = closeableHttpClient.execute(httpPost);
-
//返回结果 响应码200
-
return response.getStatusLine().getStatusCode();
-
response.close();
-
} catch (IOException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
return "error";
-
}