Using Proxy with HttpComponentsClientHttpRequestFactory and RestTemplate

后端 未结 2 910
悲&欢浪女
悲&欢浪女 2021-02-07 09:04

Can some one guide me how can I configure HttpComponentsClientHttpRequestFactory to use proxy server.

All examples I have seen are using SimpleClientH

2条回答
  •  囚心锁ツ
    2021-02-07 09:32

    Very simple way to let the HttpComponentsClientHttpRequestFactory use the standard java SystemProperties for proxy-stuff (see https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html)

    is this:

            HttpRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
    
        HttpClient httpClient = HttpClientBuilder
                .create()
                .setRoutePlanner(routePlanner)
                .build();
        restTemplate.setRequestFactory(
                new HttpComponentsClientHttpRequestFactory(httpClient));
    

    That way, it even regards the nonProxyHosts setting.

提交回复
热议问题