252. Setting a proxy

To set up a proxy, we rely on the HttpClient.proxy() method of a Builder method. The proxy() method gets an argument of the ProxySelector type, which can be the system-wide proxy selector (via getDefault()) or the proxy selector that's pointed to via its address (via InetSocketAddress).

Let's assume that we have a proxy at the proxy.host:80 address. We can set up this proxy as follows:

HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("proxy.host", 80)))
.build();

Alternatively, we can set up the system-wide proxy selector, as follows:

HttpClient client = HttpClient.newBuilder()
.proxy(ProxySelector.getDefault())
.build();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.196.244