繁体   English   中英

在Apache HTTP客户端中传递属性

[英]passing attributes in apache http client

我的卷毛如下图所示

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密钥库中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM