2016-11-29 97 views
5

關於代理服務器設置的文檔Unirest提到使用:如何設置Unirest代理的憑據?

Unirest.setProxy(新HttpHost( 「127.0.0.1」,8000));

由於我使用的代理服務器需要身份驗證,所以我需要指定憑據,但我看不到如何。

回答

6

我解決了它設置自定義客戶端,而不是代理。

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