我正在使用HttpClient登录网站。但我也登录了具有自签名证书的网站。我解决了这两个问题但我不能同时实现它们。
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("www.website.com", x),
new UsernamePasswordCredentials("username", "password"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
这允许我登录网站。
HttpClientBuilder cb = HttpClientBuilder.create();
SSLContextBuilder sslcb = new SSLContextBuilder();
sslcb.loadTrustMaterial(KeyStore.getInstance(KeyStore.getDefaultType()),
new TrustSelfSignedStrategy());
cb.setSslcontext(sslcb.build());
然后,这允许我访问任何具有任何证书的站点(是的,我知道安全风险)
但是当我尝试一起实施它们时
CloseableHttpClient httpc = HttpClients.custom() .setSslcontext(sslcb.build()).setDefaultCredentialsProvider(credsProvider)
.build();
它不会工作。我收到错误。如果有人能把正确的方向发送给我,将它们放在一起会非常感激。