有关代理设置的Unirest文档提到要使用:

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

由于我正在使用的代理服务器需要身份验证,因此我也需要指定凭据,但我不知道如何。

最佳答案

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

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

07-26 09:31