简体   繁体   中英

java https no verify certificate

I would like to connect to a test server in https from a java program I made. I don't want to verify anything in the certificate, how can I achieve this?

I am using:

HttpURLConnection connection = (HttpURLConnection) ( new URL(server).openConnection() );
                            connection.setDoOutput       (true);
                            connection.setDoInput        (true);
                            connection.setInstanceFollowRedirects(false); 
                            connection.setRequestMethod("POST"); 
          OutputStream        out  = connection.getOutputStream();
          OutputStreamWriter  wout = new OutputStreamWriter(out, "UTF-8");
                              wout.write(xml);
                              wout.flush();
                              out .close();




          //READ RESPONSE.
          InputStream in = connection.getInputStream();

But when I execute, I get:

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present

Generally you can acces https sites but Somesites wanted the certificate. So you can use under the codes. And you have to take certificate with InstallCert program.

String httpsURL = "https://www.google.com";
URL myurl = new URL(httpsURL);
                HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
                InputStream ins = con.getInputStream();
                InputStreamReader isr = new InputStreamReader(ins);
                BufferedReader in = new BufferedReader(isr);

You can do that this way..

URL url1;
try {
            url1 = new URL(url);

            if(url1.getProtocol().equalsIgnoreCase("https")){// you dont need this check
                try {
                HostnameVerifier hv = new HostnameVerifier() {

                       public boolean verify(String urlHostName, javax.net.ssl.SSLSession session) {

                           if (urlHostName.equals(session.getPeerHost())) {
                               logger.info("Verified HTTPS "+session.getPeerHost()+"  >> "+ urlHostName);
                           } else {
                               logger.info("Warning: URL host "+urlHostName+" is different to SSLSession host "+session.getPeerHost());
                           }
                           return true;
                       }
                   };

                   TrustManager[] trustAll = new javax.net.ssl.TrustManager[] { new javax.net.ssl.X509TrustManager() {

                       public java.security.cert.X509Certificate[] getAcceptedIssuers() {

                           return null;
                       }

                       public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {

                       }

                       public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {

                       }
                   } };

                   javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");

                    sc.init(null, trustAll, new java.security.SecureRandom());

                   SSLSocketFactory factory = (SSLSocketFactory) sc.getSocketFactory();
                   HttpsURLConnection.setDefaultSSLSocketFactory(factory);
                   HttpsURLConnection.setDefaultHostnameVerifier(hv);

                    HttpsURLConnection connection = (HttpsURLConnection)                 url1.openConnection();
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setRequestMethod("POST");
                    connection.setUseCaches(false);     

Continue with your OutputStreamWriter write part. You can do this without importing ssl certificate and without any third party support.

如果您尝试从https:// IP / file获取 ,它将返回未验证IP的错误

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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