Apache HttpClient 4.1 - Proxy Authentication

67,592

Solution 1

For Basic-Auth it looks like this:

DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
    new AuthScope("PROXY HOST", 8080),
    new UsernamePasswordCredentials("username", "password"));

HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https");
HttpHost proxy = new HttpHost("PROXY HOST", 8080);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

AFAIK NTLM is not supported out of the box. But you might be able to manage that using NTCredentials and maybe overloading DefaultProxyAuthenticationHandler.

Solution 2

For anyone looking for the answer for 4.3...its fairly new and their example didn't use the new HttpClientBuilder...so this is how I implemented this in that version:

NTCredentials ntCreds = new NTCredentials(ntUsername, ntPassword,localMachineName, domainName );

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( new AuthScope(proxyHost,proxyPort), ntCreds );
HttpClientBuilder clientBuilder = HttpClientBuilder.create();

clientBuilder.useSystemProperties();
clientBuilder.setProxy(new HttpHost(pxInfo.getProxyURL(), pxInfo.getProxyPort()));
clientBuilder.setDefaultCredentialsProvider(credsProvider);
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());

CloseableHttpClient client = clientBuilder.build();

Solution 3

Instead of NTLM one can use just plain old username and password on 4.3+ httpClient, as follows:

HttpHost proxy = new HttpHost("x.x.com",8080);
Credentials credentials = new UsernamePasswordCredentials("username","password");
AuthScope authScope = new AuthScope("x.x.com", 8080);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(authScope, credentials);
HttpClient client = HttpClientBuilder.create().setProxy(proxy).setDefaultCredentialsProvider(credsProvider).build();
HttpResponse response=client.execute(new HttpGet("http://stackoverflow.com/questions/6962047/apache-httpclient-4-1-proxy-authentication"));

Solution 4

How to setup proxy authentication using Apache's httpclient

(Pre-authorization on proxy networks)

This answer uses Apache's HttpClient v4.1 and later.

The accepted answer didn't work for me, but I found something else that did!

Here's some tested, verified code from apache that demonstrates how to authenticate through a proxy for a HTTP request.

The full documentation is located here: https://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html .

There's also an excellent example from Apache here: https://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientProxyAuthentication.java

  • Replace my_username with your proxy username
  • Replace my_password with your proxy password
  • Replace proxy.mycompany.com with your proxy host
  • Replace 8080 with your proxy port
  • Replace google.com with the host of the site that you want to send your HTTP request to.
  • Replace /some-path with the path that you want to send the HTTP request to. This uses the host site you specified earlier (google.com).

The following example will authenticate username:[email protected]:8080 and send a GET request to http://www.google.com/some-path and will print the response HTTP code.

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(
            new AuthScope("proxy.mycompany", 8080),
            new UsernamePasswordCredentials("my_username", "my_password"));
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCredentialsProvider(credsProvider).build();
    try {
        //Replace "google.com" with the target host you want to send the request to
        HttpHost target = new HttpHost("google.com", 80, "http");
        HttpHost proxy = new HttpHost("proxy.mycompany", 8080);

        RequestConfig config = RequestConfig.custom()
            .setProxy(proxy)
            .build();
        CloseableHttpResponse response = null;

        //Replace "/some-path" with the path you want to send a get request to.
        HttpPost httppost = new HttpPost("/some-path");
        httppost.setConfig(config);
        response = httpclient.execute(target, httppost);

        try {
            System.out.println("Return status code is "+response.getStatusLine().getStatusCode());          
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
Share:
67,592
Daniel C. Sobral
Author by

Daniel C. Sobral

I have been programming for more than 20 years now, starting with 8 bits computers, assembler and BASIC. My passion for languages meant that, by the time I entered college, I had already programmed for fun or profit in more than 20 languages, including odd ones like Forth, MUMPS and APL, as well as theoretically important ones like Lisp and Prolog. Some of my code ended up in FreeBSD, of which I was a committer for some years, while I got my masters degree in the field of distributed algorithms. I also contributed to Scala, with small amounts of code, some reasonable amount of documentation, and a couple of years of a lot of attention to the Scala questions on Stack Overflow.

Updated on February 04, 2020

Comments

  • Daniel C. Sobral
    Daniel C. Sobral over 4 years

    I've been trying to configure the user and password for proxy authentication from the configured properties while using Apaches HttpComponent's httpclient, but with no success. All examples I have found refer to methods and classes that are no longer available, such as HttpState and setProxyCredentials.

    So, can anyone give me an example of how to configure the proxy credentials?