如何通过https从受密码保护的URL下载目录?

时间:2015-07-31 11:22:55

标签: java url ssl

我有一台带有“my_ip”的服务器,其中SSL是通过apache2启用的。它也受密码保护。我有用户名和密码。当我输入https://my_ip/com/dir1/dir2时,它会提示我输入凭据。现在我想通过java将dir2或dir2本身的内容下载到我的机器上。我该怎么做?

我尝试了以下方法,

Authenticator.setDefault(new Authenticator()
             {
      protected PasswordAuthentication getPasswordAuthentication() {
        String prompt = getRequestingPrompt();
        System.out.println(prompt);
        String host = getRequestingHost();
        System.out.println(host);
        InetAddress adresseIP = getRequestingSite();
        System.out.println(adresseIP);
        int port = getRequestingPort();
        System.out.println (port);
        String username = "username";
        String password = "password";
        return new PasswordAuthentication(username, password.toCharArray());
      }
    });
        URL url = new URL("https://my_ip/com/dir1/dir2/test.xml");
        // print URL Content
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        while ((str = in.readLine()) != null) {
          System.out.println(str);
        }
        in.close();
      }

但是我收到以下错误:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1902)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1338)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:154)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1032)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1328)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:515)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1299)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at java.net.URL.openStream(URL.java:1037)
    at Test.main(Test.java:41)
Caused by: java.security.cert.CertificateException: No subject alternative names present
    at sun.security.util.HostnameChecker.matchIP(HostnameChecker.java:142)
    at sun.security.util.HostnameChecker.match(HostnameChecker.java:91)
    at sun.security.ssl.X509TrustManagerImpl.checkIdentity(X509TrustManagerImpl.java:347)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:203)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1320)
    ... 13 more

我在这里做错了什么?

1 个答案:

答案 0 :(得分:-1)

试试这个。

String url = "https://selfsolve.apple.com/wcResults.do";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

有关详细信息,请参阅this site