23
votes

I am using Spring 5 WebClient. I want to know if it is possible to configure it to use an HTTP Proxy, or if there is a way of changing it's default configuration to do so.

2

2 Answers

35
votes

This is something that the underlying client library should support.

When using Reactor Netty, you can do something like:

HttpClient httpClient = HttpClient.create()
            .tcpConfiguration(tcpClient ->
                    tcpClient.proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP).host("myproxyhost")));
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);
WebClient client = WebClient.builder().clientConnector(connector).build();
1
votes

" tcpConfiguration" is deprecated. So used this part of code instead.

  HttpClient httpClient =
            HttpClient.create()
                    .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
                            .host(sasConfig.getProxyHost())
                            .port(Integer.parseInt(sasConfig.getProxyPort())));

    ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);

    WebClient webClient =  WebClient.builder().clientConnector(connector).build();