How to get last redirect URL in Spring WebClient

旧时模样 提交于 2021-01-29 11:23:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!