gpt4 book ai didi

spring - 使用 httpClient 将代理信息和基本身份验证添加到 Resttemplate

转载 作者:行者123 更新时间:2023-12-02 04:55:45 31 4
gpt4 key购买 nike

我的开发环境位于代理后面,因此我需要将代理信息设置为其余模板,当我使用 HttpComponentsClientHttpRequestFactory 并在 httpClient 中设置代理设置并在模板中设置它时,这一切都很好。

但现在我有一个需要基本身份验证的休息服务。要设置基本身份验证凭据,我需要在其余模板上的 httpClient 中设置它们。但我发现 httpClient 中的 getparams 方法已被废弃,因此我不能只更新模板中的现有客户端,如果我创建一个新的 httpclient 对象,我将覆盖在应用程序引导期间设置的代理信息。

那么有什么方法可以让我从其余模板中提取 httpClient 并更新它吗?或者有其他方法可以解决这个问题吗?

谢谢。

最佳答案

配置httpClient如下:

HttpHost target = new HttpHost("hostname", 80, "http");
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(target.getHostName(), target.getPort()),
new UsernamePasswordCredentials("user", "passwd"));

HttpHost proxy = new HttpHost("proxy", 12345);
CloseableHttpClient httpclient = HttpClients.custom()
.setProxy(proxy)
.setDefaultCredentialsProvider(credsProvider).build();

HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpclient);

RestTemplate restTemplate = new RestTemplate(requestFactory);

另请参阅HttpClient Examples

关于spring - 使用 httpClient 将代理信息和基本身份验证添加到 Resttemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36056383/

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