2013-12-15 111 views
0

我想從我製作的java程序中通過https連接到測試服務器。我不想驗證證書中的任何內容,我怎樣才能做到這一點?java https沒有驗證證書

我使用:

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(); 

但是,當我執行,我得到:

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

回答

0

一般來說,你可以存取權限https網站,但Somesites想要的證書。所以你可以在代碼下使用。你必須拿到InstallCert程序的證書。

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); 
0

你可以做這樣..

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);  

與OutputStreamWriter寫入部分繼續。 您可以在不導入SSL證書且沒有任何第三方支持的情況下執行此操作。

0

,如果你想從https://IP/file得到的,它會返回IP未驗證

+0

歡迎堆棧溢出錯誤,請[乘觀光(https://stackoverflow.com/tour) ,請確保你閱讀了[我如何寫出一個好的答案?](https://stackoverflow.com/help/how-to-answer)並更新了答案並提供了更多信息。 – lordrhodos