如何从 RestTemplate 重定向 url?
How to get redirected url from RestTemplate?
我想用 http GET 调用 RestTemplate 并检索状态代码并重定向 url(如果有的话)。
如何实现?
- 使用自定义
RedirectStrategy
创建 Apache HttpClient
,您可以在其中拦截发生重定向时的中间响应。
- 将默认请求工厂替换为
HttpComponentsClientHttpRequestFactory
和新的 Apache HttpClient
。
查看 org.apache.http.client.RedirectStrategy 了解更多信息。或者重复使用 DefaultRedirectStrategy
,如下例所示:
CloseableHttpClient httpClient = HttpClientBuilder
.create()
.setRedirectStrategy( new DefaultRedirectStrategy() {
@Override
public boolean isRedirected(HttpRequest request, HttpResponse response,
HttpContext context) throws ProtocolException {
System.out.println(response);
// If redirect intercept intermediate response.
if (super.isRedirected(request, response, context)){
int statusCode = response.getStatusLine().getStatusCode();
String redirectURL = response.getFirstHeader("Location").getValue();
System.out.println("redirectURL: " + redirectURL);
return true;
}
return false;
}
})
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
.......
var responseEntity = restTemplate.exchange("someurl", HttpMethod.GET, null, Object.class);
HttpHeaders headers = responseEntity.getHeaders();
URI location = headers.getLocation();
location.toString();
使用 Spring 并覆盖 SimpleClientHttpRequestFactory 中的 prepareConnection()...
RestTemplate restTemplate = new RestTemplate( new SimpleClientHttpRequestFactory(){
@Override
protected void prepareConnection( HttpURLConnection connection, String httpMethod ) {
connection.setInstanceFollowRedirects(false);
}
} );
ResponseEntity<Object> response = restTemplate.exchange( "url", HttpMethod.GET, null, Object.class );
int statusCode = response.getStatusCodeValue();
String location = response.getHeaders().getLocation() == null ? "" : response.getHeaders().getLocation().toString();
我想用 http GET 调用 RestTemplate 并检索状态代码并重定向 url(如果有的话)。
如何实现?
- 使用自定义
RedirectStrategy
创建 ApacheHttpClient
,您可以在其中拦截发生重定向时的中间响应。 - 将默认请求工厂替换为
HttpComponentsClientHttpRequestFactory
和新的 ApacheHttpClient
。
查看 org.apache.http.client.RedirectStrategy 了解更多信息。或者重复使用 DefaultRedirectStrategy
,如下例所示:
CloseableHttpClient httpClient = HttpClientBuilder
.create()
.setRedirectStrategy( new DefaultRedirectStrategy() {
@Override
public boolean isRedirected(HttpRequest request, HttpResponse response,
HttpContext context) throws ProtocolException {
System.out.println(response);
// If redirect intercept intermediate response.
if (super.isRedirected(request, response, context)){
int statusCode = response.getStatusLine().getStatusCode();
String redirectURL = response.getFirstHeader("Location").getValue();
System.out.println("redirectURL: " + redirectURL);
return true;
}
return false;
}
})
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
.......
var responseEntity = restTemplate.exchange("someurl", HttpMethod.GET, null, Object.class);
HttpHeaders headers = responseEntity.getHeaders();
URI location = headers.getLocation();
location.toString();
使用 Spring 并覆盖 SimpleClientHttpRequestFactory 中的 prepareConnection()...
RestTemplate restTemplate = new RestTemplate( new SimpleClientHttpRequestFactory(){
@Override
protected void prepareConnection( HttpURLConnection connection, String httpMethod ) {
connection.setInstanceFollowRedirects(false);
}
} );
ResponseEntity<Object> response = restTemplate.exchange( "url", HttpMethod.GET, null, Object.class );
int statusCode = response.getStatusCodeValue();
String location = response.getHeaders().getLocation() == null ? "" : response.getHeaders().getLocation().toString();