简体   繁体   English

尝试使用HTTPclient接受SSL证书时,为什么会收到连接重置?

[英]Why do I receive a connection reset when I tried to accept my SSL certificate using HTTPclient?

I tried to accept my certificate to test my REST api. 我试图接受我的证书来测试我的REST API。 For that, I tried : 为此,我尝试了:

SSLContext sslcontext = SSLContexts
        .custom()
        .loadTrustMaterial(new File(KEYSTORE_PATH), KEYSTORE_PASSWORD,
                new TrustSelfSignedStrategy()).build();

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
        sslcontext, new String[] { "TLSv1" }, null,
        SSLConnectionSocketFactory.getDefaultHostnameVerifier());

CloseableHttpClient closeableHttpClient = HttpClients.custom()
        .setSSLSocketFactory(sslsf)
        .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();
try {
    HttpPost postRequest = new HttpPost(BASE_URL);

    CloseableHttpResponse response = closeableHttpClient
            .execute(postRequest);
    // try {
    HttpEntity entity = response.getEntity();

    EntityUtils.consume(entity);
    // } finally {
    // response.close();
    // }
} finally {
    closeableHttpClient.close();
}

The error happen when I do closeableHttpClient.execute(postRequest); 当我执行closeableHttpClient.execute(postRequest);时发生错误closeableHttpClient.execute(postRequest);

Variables are : 变量是:

private static final String KEYSTORE_PATH = System.getProperty("java.home")
        + "/lib/security/cacerts".replace('/', File.separatorChar);
private static final char[] KEYSTORE_PASSWORD = "changeit".toCharArray();
private static final String BASE_URL = "http://localhost:9010/api";

note : I do not have a domain name, i'm on localhost. 注意:我没有域名,我在localhost上。 If I understood the error well it's my SSLConnectionSocketFactory closed too early. 如果我很好地理解了错误,那是我的SSLConnectionSocketFactory关闭得太早了。 But I don't know how and why. 但是我不知道如何以及为什么。

EDIT : I'm using HTTPClient. 编辑:我正在使用HTTPClient。 If I update the port HTTP to HTTPS, I receive another error : javax.net.ssl.SSLPeerUnverifiedException: Host name 'localhost' does not match the certificate subject provided by the peer 如果将端口HTTP更新为HTTPS, javax.net.ssl.SSLPeerUnverifiedException: Host name 'localhost' does not match the certificate subject provided by the peer收到另一个错误: javax.net.ssl.SSLPeerUnverifiedException: Host name 'localhost' does not match the certificate subject provided by the peer

I found the solution here : javax.net.ssl.SSLPeerUnverifiedException: Host name does not match the certificate subject provided by the peer My code is now : 我在这里找到解决方案: javax.net.ssl.SSLPeerUnverifiedException:主机名与对等方提供的证书主题不匹配我的代码现在是:

SSLContext sslcontext = SSLContexts
        .custom()
        .loadTrustMaterial(new File(KEYSTORE_PATH), KEYSTORE_PASSWORD,
                new TrustSelfSignedStrategy()).build();

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
        sslcontext, NoopHostnameVerifier.INSTANCE);

final Registry<ConnectionSocketFactory> registry = RegistryBuilder
        .<ConnectionSocketFactory> create()
        .register("http", new PlainConnectionSocketFactory())
        .register("https", sslsf).build();

final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
        registry);
cm.setMaxTotal(100);

It's working but I'm still working on it (understand and find if I can do something else/better)! 它正在工作,但我仍在努力(了解并确定我是否可以做其他事情/更好)! Thanks 谢谢

暂无
暂无

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

相关问题 如果我的客户端程序是Java而不是C#,为什么必须添加服务器SSL证书? - Why do I have to add the server SSL certificate if my client program is in Java and not when it is in C#? 如何将 SSL 客户端证书与 Apache HttpClient 一起使用? - How do I use an SSL client certificate with Apache HttpClient? 如何在Apache客户端上接受过期的ssl证书? - How do I accept an expired ssl certificate with Apache client? Apache httpclient 4.3.3如何只接受一个特定的自签名证书 - Apache httpclient 4.3.3 how do I accept only one specific self signed certificate 如何使用 keytools 将我的 VPS SSL 证书导入到我的码头密钥库中? - How do I import my VPS SSL certificate into my jetty keystore using keytools? 当我进行空检查时,为什么还会在IntelliJ中收到可能的NPE警告? - Why do I still receive warning for possible NPE in IntelliJ when I do my null checking? 如何使用StartTLS获取LDAP服务器的SSL证书? - How do I get the SSL certificate for an LDAP server using StartTLS? 为什么我得到java.net.SocketException:部署app时连接重置? - Why do I get java.net.SocketException: Connection reset when deploying app? 在java中使用Apache HttpClient时如何修复“连接重置” - How to fix "Connection Reset" when using Apache HttpClient in java 如何使用HttpsUrlConnection对SSL连接使用代理身份验证? - How do I use proxy authentication with an SSL connection using HttpsUrlConnection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM