gpt4 book ai didi

java - 如何为 Unirest 代理设置凭据?

转载 作者:搜寻专家 更新时间:2023-10-31 19:37:09 27 4
gpt4 key购买 nike

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

关于java - 如何为 Unirest 代理设置凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40864167/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com