HttpClient
HttpClient Get方式请求接口
public JSONObject getProducts(String token) {
JSONObject jsonObject = null;
//创建HttpClient对象
HttpClient objClient = new HttpClient();
//设置传递的编码格式,防止出现乱码错误
objClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
//创建GetMethod 对象,参数为请求的地址
GetMethod method = new GetMethod("https://XXXXX.com/");
//设置请求头
method.setRequestHeader("token", token);
try {
//执行方法请求接口
objClient.executeMethod(method);
//打印服务器返回的状态
System.out.println(method.getStatusLine());
//获取返回值信息
String strResponseBody = new String(method.getResponseBody(),"UTF-8");
System.out.println(strResponseBody);
jsonObject = JSONObject.parseObject(strResponseBody);
} catch (IOException e) {
e.printStackTrace();
}
//释放连接
method.releaseConnection();
return jsonObject;
}
HttpClient Post方式请求接口
public JSONObject getProducts(String token) {
JSONObject jsonObject = null;
//创建HttpClient对象
HttpClient objClient = new HttpClient();
//设置传递的编码格式,防止出现乱码错误
objClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
//创建PostMethod 对象,参数为请求的地址
PostMethod method = new PostMethod("https://XXXXX.com/");
//设置请求头
method.setRequestHeader("token", token);
try {
//执行方法请求接口
objClient.executeMethod(method);
//打印服务器返回的状态
System.out.println(method.getStatusLine());
//获取返回值信息
String strResponseBody = new String(method.getResponseBody(),"UTF-8");
System.out.println(strResponseBody);
jsonObject = JSONObject.parseObject(strResponseBody);
} catch (IOException e) {
e.printStackTrace();
}
//释放连接
method.releaseConnection();
return jsonObject;
}
CloseableHttpClient
CloseableHttpClient Get方式请求接口
public JSONObject getProducts(String token) {
JSONObject jsonObject = null;
//通过HttpClients.createDefault()获取到CloseableHttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//创建HttpGet 对象,参数为请求的地址
HttpGet method = new HttpGet("https://XXXXX.com/");
//设置请求头
method.setHeader("token", token);
try {
//执行方法请求接口得到响应
HttpResponse response = httpclient.execute(method);
//解析响应数据
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
//转为json格式
jsonObject = JSON.parseObject(content);
System.out.println(jsonObject.toString());
EntityUtils.consume(response.getEntity());//完全消耗
} finally {
try {
//释放连接
method.releaseConnection();
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonObject;
}
CloseableHttpClient Post方式请求接口
public JSONObject saveOrder(String token) {
JSONObject jsonObject = null;
//通过HttpClients.createDefault()获取到CloseableHttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
//设置传递的编码格式,防止出现乱码错误
httpclient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
//创建HttpPost 对象,参数为请求的地址
HttpPost method = new HttpPost("https://XXXXX.com/");
//设置请求头
method.setHeader("token", token);
JSONObject requestJson = new JSONObject();
//日期
requestJson.put("times", new Date());
//单号,唯一
requestJson.put("orderCode", "123");
//长(厘米)
requestJson.put("longs", "1");
//宽(厘米)
requestJson.put("width", "1");
//高(厘米
requestJson.put("high", "1");
//转为StringEntity
StringEntity entity = new StringEntity(requestJson.toString(), Charset.forName("UTF-8"));
//设置编码
entity.setContentEncoding("UTF-8");
// 发送Json格式的数据请求
entity.setContentType("application/json");
//设置参数
method.setEntity(entity);
try {
//执行方法请求接口得到响应
HttpResponse response = httpclient.execute(method);
//获取返回值信息
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
//转为json
jsonObject = JSON.parseObject(content);
System.out.println(jsonObject.toString());
EntityUtils.consume(response.getEntity());//完全消耗
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//释放连接
method.releaseConnection();
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonObject;
}