Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set credentials for Unirest proxy?

Tags:

java

unirest

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.

like image 913
Luca S. Avatar asked Nov 29 '16 10:11

Luca S.


1 Answers

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());
like image 191
Luca S. Avatar answered Nov 11 '22 06:11

Luca S.