如何为 Unirest 代理设置凭据?
How to set credentials for Unirest proxy?
关于代理设置的 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());
关于代理设置的 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());