RestTemplate的介绍就不说了,总的来说用这个对象可以很方便的模拟一个http请求。
Talk is cheap,Show me the code,
使用的是springboot整合的工程所以以下涉及到的对象都是注解式的声明和注入,
一、声明restTemplate对象
@Bean
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate(
getClientHttpRequestFactory());
return restTemplate;
}
@Bean
public ClientHttpRequestFactory getClientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(
getHttpClient());
factory.setConnectTimeout(3000);
factory.setReadTimeout(5000);
return factory;
}
@Bean
public ClientHttpRequestFactory getClientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(
getHttpClient());
factory.setConnectTimeout(3000);
factory.setReadTimeout(5000);
return factory;
}
@Bean
public HttpClient getHttpClient() {
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
return 5 * 1000;//设置一个链接的最大存活时间
}
};
return HttpClientBuilder.create().setConnectionManager(getManager())
.setKeepAliveStrategy(myStrategy).build();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
当然,可以利用注解@Bean的name属性指定实例化对象的name,注入的时候使用@Qualifier("Name of restTemplate")指定name即可。
在平常使用中如果不想启动spring的ioc,并且想简单使用restTemplate 可以直接使用new,但是要注意使用中发出去一个请求之后有可能链接断了之后那么程序会永远的阻塞在那了,切记。
RestTemplate restTemplate = new RestTemplate();
- 1
- 1
在使用中要在适合的时机去清空超时或者空白的链接
@Autowired
private PoolingHttpClientConnectionManager manager;
...............
connectionManager.closeExpiredConnections();
connectionManager.closeIdleConnections(10, TimeUnit.SECONDS);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
下面展示header的设置以及一个完整的交互过程
public String getPolicyJson(String policyNo){
HttpHeaders httpHeaders = new HttpHeaders();
//设置header
httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
Map<String, String> hashmap = new LinkedHashMap<String, String>();
int random = RandomUtils.nextInt(0, 100000);
hashmap.put("random", random+"");
hashmap.put("orderNo", policyNo);
hashmap.put("requestSource","");
HttpEntity<Map<String, String>> requestEntity = new HttpEntity<Map<String, String>>(
hashmap, httpHeaders);
ResponseEntity<String> resp = restTemplate.exchange(queryUrlJson
,HttpMethod.POST,requestEntity, String.class);
//获取返回的header
List<String> val = resp.getHeaders().get("Set-Cookie");
String body = resp.getBody();
return body;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
上面代码的hashmap一个字段只能传输一个值,可以使用org.springframework.util.LinkedMultiValueMap.LinkedMultiValueMap<String,String>()这个类的对象传输,一般情况下是与上述代码的结果一样的
restTemplate有许多get和post开头的方法,可以都试试啊说不定就遇到了个最喜欢的了,
附,
如果返回结果是html,可以使用Jsoup的Document 进行解析