Luca S. Luca S. - 8 days ago 5
Java Question

How to set credentials for Unirest proxy?

Documentation for Unirest about proxy settings mentions to use:


Unirest.setProxy(new HttpHost("127.0.0.1", 8000));


Since the proxy I'm using requires authentication I would need to specify the credentials too but I don't see how.

Answer

I solved it setting a custom client instead of the proxy.

HttpClientBuilder clientBuilder = HttpClientBuilder.create();


  CredentialsProvider credsProvider = new BasicCredentialsProvider();

  credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USERNAME, PASSWORD));

  clientBuilder.useSystemProperties();

  clientBuilder.setProxy(new HttpHost(HOST, PORT));
            clientBuilder.setDefaultCredentialsProvider(credsProvider);
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());


 Lookup<AuthSchemeProvider> authProviders = RegistryBuilder.<AuthSchemeProvider>create()
                    .register(AuthSchemes.BASIC, new BasicSchemeFactory())
                    .build();
            clientBuilder.setDefaultAuthSchemeRegistry(authProviders);


    Unirest.setClient(clientBuilder.build());