Hi, how do I configure Apache HttpClient to bypass proxies for local addresses?

I configure the client as follows:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

Now I would like to tell my client not to use a proxy for "localhost" or 127.0.0.1.

Thanks!

+4
source share
1 answer

Using HttpClient 4.3 APIs

HttpHost proxy = new HttpHost("someproxy", 8080);
HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy) {

    @Override
    public HttpRoute determineRoute(
            final HttpHost host,
            final HttpRequest request,
            final HttpContext context) throws HttpException {
        String hostname = host.getHostName();
        if (hostname.equals("127.0.0.1") || hostname.equalsIgnoreCase("localhost")) {
            // Return direct route
            return new HttpRoute(host);
        }
        return super.determineRoute(host, request, context);
    }
};
CloseableHttpClient client = HttpClients.custom()
        .setRoutePlanner(routePlanner)
        .build();
+9
source

Source: https://habr.com/ru/post/1525260/


All Articles