问题
A client that follows redirects can be created as follows:
WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(
HttpClient.create().followRedirect(true)
))
After invoking a HEAD
request on a URL, how can the final Location
header be retrieved? In other words, how can we get the final URL redirected to?
回答1:
It is true that HttpClient#followRedirect(true)
enables the redirection.
However there is also HttpClient#followRedirect(BiPredicate<HttpClientRequest,HttpClientResponse>)
, here you can control more precisely when you want to redirect and in addition to this you have always access to the response and the Location
header, so in any time you will know to which location there will be a redirection.
More info here and here
For example
WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(
HttpClient.create().followRedirect((req, res) -> {
System.out.println(res.responseHeaders().get("Location"));
return HttpResponseStatus.FOUND.equals(res.status());
})
))
来源:https://stackoverflow.com/questions/57468043/how-to-get-last-redirect-url-in-spring-webclient