java 远程调用第三方服务

1.httpclient

2.RestTemplate

private static ResultIf<PdfPlanIdsDto> get(String url) {
ResultIf<PdfPlanIdsDto> pdfPlanIdsDto = null;
try {
log.info("url={}", url);
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", "application/json");
headers.add("Content-Encoding", "UTF-8");
headers.add("Content-Type", "application/json; charset=UTF-8");
org.springframework.http.HttpEntity<String> requestEntity = new org.springframework.http.HttpEntity<>(null, headers);
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,
requestEntity, String.class);
if (response.getStatusCode().equals(HttpStatus.OK)) {
String body = response.getBody();
      //转对象
pdfPlanIdsDto= JSON.parseObject(body, new TypeReference<ResultIf<PdfPlanIdsDto>>() {
});
}
} catch (Exception e) {
log.error("{},请求服务器异常:{}", url,e);
}
return pdfPlanIdsDto;
}
上一篇:爬虫初步探索


下一篇:前端知识点 | 查看已登录网站 Cookie 信息