如何设置Unirest代理的凭据?

时间:2016-11-29 10:59:22

标签: java unirest

Unirest关于代理设置的文档提及使用:

  

Unirest.setProxy(new HttpHost(“127.0.0.1”,8000));

由于我使用的代理需要身份验证,我还需要指定凭据,但我不知道如何。

1 个答案:

答案 0 :(得分:8)

我解决了它设置自定义客户端而不是代理。

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());