Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 504
galusben Avatar asked Feb 04 '14 16:02

galusben


People also ask

What is HttpClient proxy?

Hyper Text Transfer Protocol (HTTP) is a request/response protocol between clients and servers. The HTTP client is usually a web browser. The HTTP server is a remote resource that stores HTML files, images, and other content.

What is closable HttpClient?

CloseableHttpClient is an abstract class which is the base implementation of HttpClient that also implements java. io. Closeable.

Do we need to close HttpClient connection?

If you are processing HTTP responses manually instead of using a response handler, you need to close all the http connections by yourself.


1 Answers

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();
like image 116
ok2c Avatar answered Sep 23 '22 15:09

ok2c