创建 https 连接时出现 java.net.SocketException,javax.net.ssl.DefaultSSLSocketFactory.createSocket

发布于 2024-09-25 20:02:04 字数 2315 浏览 2 评论 0原文

我正在尝试创建一个 https 连接,并且它在测试类中工作正常。但是当我在操作类中调用相同的方法时,它在步骤 666666666666 之后出现以下错误。

java.net.SocketException 在 javax.net.ssl.DefaultSSLSocketFactory.createSocket(SSLSocketFactory.java:8) 在 com.ibm.net.ssl.www2.protocol.https.b.afterConnect(b.java:29) 在 com.ibm.net.ssl.www2.protocol.https.c.connect(c.java:34) 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:943) 在 com.ibm.net.ssl.www2.protocol.https.a.getInputStream(a.java:13) 处,

方法如下。

公共静态字符串登录(字符串reqXML){

    String responseXml="";
    try {
        JConfigUtil configuratorTest = JConfigUtil.getInstance();
        String LogOn_URL = configuratorTest.getProperty(CenturionConst.Centurion,CenturionConst.LogOnService_URL);
    //  System.out.println("Log On URL :"+LogOn_URL);
        String param = URLEncoder.encode(reqXML,"UTF8");
        String inputUrl=LogOn_URL+param;
        System.out.println("11111111111111111");
        System.setProperty("javax.net.ssl.trustStore", CERT_FILE_LOC);

        System.out.println("22222222222222222");

        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                System.out.println("Warning: URL Host: "+urlHostName+" vs. "+session.getPeerHost());
                return true;
            }
        };
        System.out.println("33333333333333333");
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
        System.out.println("444444444444444444");
        System.out.println(inputUrl);
        URL url = new URL(inputUrl);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        System.out.println("555555555555555555");
        connection.setDoOutput(true);
        System.out.println("6666666666666666666");
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        System.out.println("77777777777777777777");
        while ((line = in.readLine()) != null) {
            responseXml=responseXml+line;
            }
        in.close();
        System.out.println("8888888888888888888888");
    }  catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return responseXml;
}

I am trying to create a https connection and its working fine in a test class.but when i call the same method in action class its getting following error after step 666666666666.

java.net.SocketException
at javax.net.ssl.DefaultSSLSocketFactory.createSocket(SSLSocketFactory.java:8)
at com.ibm.net.ssl.www2.protocol.https.b.afterConnect(b.java:29)
at com.ibm.net.ssl.www2.protocol.https.c.connect(c.java:34)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:943)
at com.ibm.net.ssl.www2.protocol.https.a.getInputStream(a.java:13)

the method is following.

public static String logOn(String reqXML){

    String responseXml="";
    try {
        JConfigUtil configuratorTest = JConfigUtil.getInstance();
        String LogOn_URL = configuratorTest.getProperty(CenturionConst.Centurion,CenturionConst.LogOnService_URL);
    //  System.out.println("Log On URL :"+LogOn_URL);
        String param = URLEncoder.encode(reqXML,"UTF8");
        String inputUrl=LogOn_URL+param;
        System.out.println("11111111111111111");
        System.setProperty("javax.net.ssl.trustStore", CERT_FILE_LOC);

        System.out.println("22222222222222222");

        HostnameVerifier hv = new HostnameVerifier() {
            public boolean verify(String urlHostName, SSLSession session) {
                System.out.println("Warning: URL Host: "+urlHostName+" vs. "+session.getPeerHost());
                return true;
            }
        };
        System.out.println("33333333333333333");
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
        System.out.println("444444444444444444");
        System.out.println(inputUrl);
        URL url = new URL(inputUrl);
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        System.out.println("555555555555555555");
        connection.setDoOutput(true);
        System.out.println("6666666666666666666");
        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        System.out.println("77777777777777777777");
        while ((line = in.readLine()) != null) {
            responseXml=responseXml+line;
            }
        in.close();
        System.out.println("8888888888888888888888");
    }  catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return responseXml;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-10-02 20:02:04

之前调用 trustAllHttpsCertificates()
HostnameVerifier hv = new HostnameVerifier() 它将工作

包 com.americanexpress.mobl.centurion.app.util;

导入 java.security.cert.CertificateException;
导入java.security.cert.X509Certificate;

公共类 miTM 实现 javax.net.ssl.TrustManager、javax.net.ssl.X509TrustManager{

public void checkClientTrusted(X509Certificate[] certs, String authType)
        throws CertificateException {
    // TODO Auto-generated method stub
    return;
}

public void checkServerTrusted(X509Certificate[] certs, String authType)
        throws CertificateException {
    // TODO Auto-generated method stub
    return;
}

public X509Certificate[] getAcceptedIssuers() {
    // TODO Auto-generated method stub
    return null;
} 
public boolean isClientTrusted(java.security.cert.X509Certificate[] certs)
{
    return true;
    }
public boolean isServerTrusted(java.security.cert.X509Certificate[] certs)
{
    return true;
    }

公共静态 void trustAllHttpsCertificates() 抛出异常{
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}

}

call trustAllHttpsCertificates() before
HostnameVerifier hv = new HostnameVerifier() and it ll work

package com.americanexpress.mobl.centurion.app.util;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager{

public void checkClientTrusted(X509Certificate[] certs, String authType)
        throws CertificateException {
    // TODO Auto-generated method stub
    return;
}

public void checkServerTrusted(X509Certificate[] certs, String authType)
        throws CertificateException {
    // TODO Auto-generated method stub
    return;
}

public X509Certificate[] getAcceptedIssuers() {
    // TODO Auto-generated method stub
    return null;
} 
public boolean isClientTrusted(java.security.cert.X509Certificate[] certs)
{
    return true;
    }
public boolean isServerTrusted(java.security.cert.X509Certificate[] certs)
{
    return true;
    }

public static void trustAllHttpsCertificates() throws Exception{
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文