Uploaded image for project: 'HttpComponents HttpClient'
  1. HttpComponents HttpClient
  2. HTTPCLIENT-1435

Proxy authentication of Fluent API does not work

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 4.3.1
    • 4.3.2
    • Fluent HC
    • None
    • Win7 + JDK6 + HttpClient 4.3.1 + HttpCore 4.3

    Description

      The sample code below demonstrates the issue:

      package com.test;

      import org.apache.http.HttpHost;
      import org.apache.http.auth.AuthScope;
      import org.apache.http.auth.UsernamePasswordCredentials;
      import org.apache.http.client.CredentialsProvider;
      import org.apache.http.client.config.RequestConfig;
      import org.apache.http.client.fluent.Executor;
      import org.apache.http.client.fluent.Request;
      import org.apache.http.client.methods.HttpPost;
      import org.apache.http.impl.client.BasicCredentialsProvider;
      import org.apache.http.impl.client.HttpClientBuilder;

      public class Test {
      public static void main(final String[] args) {
      final HttpHost proxy = new HttpHost("my-proxy.com", 8080);
      final String username = "username";
      final String password = "password";
      final String uri = "https://www.my-uri.com/servlet/action";

      // WORKS: HTTP/1.1 200 OK
      final CredentialsProvider credsProvider = new BasicCredentialsProvider();
      credsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(username, password));
      final HttpPost httppost = new HttpPost(uri);
      httppost.setConfig(RequestConfig.custom().setProxy(proxy).build());
      try

      { System.out.println(HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build() .execute(httppost).getStatusLine()); }

      catch (final Exception e)

      { e.printStackTrace(); }

      // WORKS: as expected: HTTP/1.1 407 Proxy Authentication Required
      try { System.out.println(Request.Post(uri).viaProxy(proxy).execute().returnResponse().getStatusLine()); } catch (final Exception e) { e.printStackTrace(); }

      // FAILS (not expected): java.net.UnknownHostException:
      try

      { final Executor executor = Executor.newInstance().auth(proxy, username, password).authPreemptive(proxy); System.out.println(executor.execute(Request.Post(uri).viaProxy(proxy)).returnResponse().getStatusLine()); }

      catch (final Exception e)

      { e.printStackTrace(); }

      }
      }

      The stacktrace below is what I get:
      HTTP/1.1 200 OK
      HTTP/1.1 407 Proxy Authentication Required
      java.net.UnknownHostException: www.my-uri.com/servlet/action
      at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
      at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:850)
      at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1201)
      at java.net.InetAddress.getAllByName0(InetAddress.java:1154)
      at java.net.InetAddress.getAllByName(InetAddress.java:1084)
      at java.net.InetAddress.getAllByName(InetAddress.java:1020)
      at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
      at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:102)
      at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:314)
      at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:357)
      at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:218)
      at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:194)
      at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
      at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
      at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
      at org.apache.http.client.fluent.Executor.execute(Executor.java:215)
      at com.test.Test.main(Test.java:44)

      Attachments

        Activity

          People

            Unassigned Unassigned
            remisbaima Remis
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: