gpt4 book ai didi

java - 在 apache http 客户端中传递属性

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:32 24 4
gpt4 key购买 nike

我有一个如下所示的 curl

curl -s -g https://example.com:8086 --tlsv1 --cacert /etc/myfolder/certificate.pem --cert /etc/myfolder/ssl/certificate.pem --key /etc/myfolder/com/certificate.pem

我已经使用 apache java http 客户端访问上面的 curl

 String url = "https://example.com:8086";
HttpClient client = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-Type", "application/json");
HttpResponse httpresponse = client.execute(httpGet);
EntityUtils.toString(httpresponse.getEntity());

但我不知道如何传递证书和属性,例如 -s -g --tlsv1 -- cacert/etc/myfolder/certificate.pem --cert/etc/myfolder/ssl/certificate.pem --key/etc/myfolder/com/certificate.pem

谁能帮我解决这个问题

最佳答案

SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(new File("/etc/myfolder/certificate.keystore"))
.loadTrustMaterial(new File("/etc/myfolder/ssl/certificate.keystore"), "store password".toCharArray(), "key password".toCharArray())
.build();
CloseableHttpClient client = HttpClientBuilder.create()
.setSSLSocketFactory(new SSLConnectionSocketFactory(
sslContext,
new String[] {"TLSv1"},
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier()))
HttpGet httpGet = new HttpGet("https://targethost/");
try (CloseableHttpResponse response1 = client.execute(httpGet)) {
System.out.println(response1.getStatusLine());
EntityUtils.consume(response1.getEntity());
}

请注意,根据 PEM 文件的格式,您可能必须先将它们加载到 Java keystore 中。

关于java - 在 apache http 客户端中传递属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41221444/

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