如何传递任何TLS或SSL安全性?

时间:2017-03-06 17:40:51

标签: groovy

所以我的代码存在一些问题,我试图连接到需要用户名和密码的网站。在我尝试连接之后,它给出了一个错误: javax.net.ssl.SSLHandshakeException:握手期间远程主机关闭连接

这是代码:

    SSLContext sslContext = SSLContexts.custom()
            .useTLS()
            .build()

    String[] aux = ["TLSv1", "TLSv1.1"]

    SSLConnectionSocketFactory f = new SSLConnectionSocketFactory(
            sslContext,
            aux,
            null,
            BROWSER_COMPATIBLE_HOSTNAME_VERIFIER)


    HttpClient client = HttpClientBuilder.create()
            .setSSLSocketFactory(f)
            .build()

    URIBuilder builder = new URIBuilder()
    builder.setScheme("https").setHost("link")
    URI uri = builder.build()
    HttpGet httpget = new HttpGet(uri)

    client.execute(httpget)

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

SSLContext sslcontext = SSLContexts.custom().build();

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

httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

HttpPost post = new HttpPost(csi_url);

StringEntity entity = new StringEntity(
    serviceXMLRequest,
    ContentType.create("text/xml", "UTF-8"));

post.setEntity(entity);             

response = httpclient.execute(post);