最近因业务需求需要从其他系统提供的RESTful接口获取数据,涉及到POST请求和GET请求,这里采用Java实现。
引入依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
发送POST请求
protected JSONObject doPost(String url, String token, JSONObject json) {
long t = new Date().getTime();
String newUrl = String.format("%s?_t=%s", url, t);
try {
RequestConfig.custom().setConnectionRequestTimeout(3000).setSocketTimeout(3000).setConnectTimeout(3000).build();
// 创建Get请求
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
httpPost.addHeader("Accept-Encoding", "gzip, deflate, br");
httpPost.addHeader("Accept-Language", "zh-CN,zh;q=0.9");
httpPost.addHeader("Connection", "keep-alive");
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
if (!StringUtils.isNotBlank(token)) {
httpPost.addHeader("token", token);
}
// httpPost.addHeader("Host", "pan.baidu.com");
// httpPost.addHeader("Origin", "https://pan.baidu.com");
// httpPost.addHeader("Referer", String.format("https://pan.baidu.com/share/init?surl=%s", id));
// Post参数
// List<NameValuePair> params = new ArrayList<>();
// params.add(new BasicNameValuePair("pwd", code));
// params.add(new BasicNameValuePair("vcode", ""));
// params.add(new BasicNameValuePair("vcode_str", ""));
// httpPost.setEntity(new UrlEncodedFormEntity(params, charset));
// 设置参数编码格式,如果不用Charset.forName("UTF-8"),服务端中文乱码
StringEntity entityRequest = new StringEntity(json.toString(), Charset.forName("UTF-8"));
// 设置参数格式为json
entityRequest.setContentType("application/json");
// GET请求的参数
httpPost.setEntity(entityRequest);
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity entityResponse = response.getEntity();
String responseContent = EntityUtils.toString(entityResponse, charset);
log.debug(responseContent);
response.close();
httpClient.close();
JSONObject jsonResponse = JSON.parseObject(responseContent);
return jsonResponse;
} catch (Exception e) {
log.error("{}.doPost throw exception: {}", this.getClass().getSimpleName(), e.getMessage());
}
return null;
}
发送GET请求
protected JSONObject doGet(String url, String token, String cookie) {
long t = new Date().getTime();
String newUrl = String.format("%s?_t=%s", url, t);
try {
RequestConfig.custom().setConnectionRequestTimeout(3000).setSocketTimeout(3000).setConnectTimeout(3000).build();
// 创建Get请求
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet http = new HttpGet(url);
http.setHeader("token", token);
CloseableHttpResponse response = httpClient.execute(http);
HttpEntity entityResponse = response.getEntity();
String responseContent = EntityUtils.toString(entityResponse, charset);
log.debug(responseContent);
response.close();
httpClient.close();
JSONObject jsonResponse = JSON.parseObject(responseContent);
return jsonResponse;
} catch (Exception e) {
log.error("{}.doGet throw exception: {}", this.getClass().getSimpleName(), e.getMessage());
}
return null;
}