Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I support an HTTP Proxy using Spring 5 WebClient?

Tags:

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.

like image 878
Taylor Gautier Avatar asked Oct 27 '17 16:10

Taylor Gautier


People also ask

How do I add a proxy to Spring Tool Suite?

Just click on Connections tab and LAN Settings button. Check Use a proxy server for your LAN (...) box and provide proxy details. Then go to Spring Tool Suite window and click on Window >> Preferences >> General >> Network Connections and choose Native from the Active Provider drop down list.

What is proxy in HTTP client?

What Does HTTP Proxy Mean? An HTTP Proxy serves two intermediary roles as an HTTP Client and an HTTP Server for security, management, and caching functionality. The HTTP Proxy routes HTTP Client requests from a Web browser to the Internet, while supporting the caching of Internet data.


2 Answers

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(); 
like image 115
Brian Clozel Avatar answered Oct 15 '22 22:10

Brian Clozel


" 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(); 
like image 34
Reza Avatar answered Oct 15 '22 22:10

Reza