Hi, how can I configure Apache HttpClient to bypass proxy for local adresses?

apache-httpclient-4.x, proxy

Solution

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();

Problem

I am configuring the client like this: ``` 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 proxy for "localhost" or 127.0.0.1. Thanks!

Original source