Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过https从受密码保护的URL下载目录?_Java_Url_Ssl - Fatal编程技术网

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

Java 如何通过https从受密码保护的URL下载目录?,java,url,ssl,Java,Url,Ssl,我有一个ip为“my_ip”的服务器,其中SSL通过apache2启用。它也有密码保护。我有用户名和密码。当我键入https://my_ip/com/dir1/dir2它将提示我输入凭据。现在我想通过java将dir2或dir2本身的内容下载到我的机器上。我该怎么做 我试过下面的方法 Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPassw

我有一个ip为“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
我做错了什么?

试试这个

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

有关更多信息,请参阅。

我想您不了解我的要求。我想从受密码保护且启用SSL的URL下载文件或目录。