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

标签 java url ssl

我有一台 IP 为“my_ip”的服务器,其中通过 apache2 启用了 SSL。它也受密码保护。我有用户名和密码。当我键入 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();

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

关于java - 如何通过 https 从受密码保护的 URL 下载目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31744981/

相关文章:

java - 单向链表添加/删除

java - 像菜单一样在另一个中打开 jFrame 的元素和结构

regex - 没有 http ://www 的 url 正则表达式

javascript - React 查询字符串采用超过 1 个参数?

ssl - libcurl : how to use TPM private key for mutual SSL authentication

java - 如何将 JSP 从主 java 项目中分离出来

java - JAVA中将Integer类型的Linked List转换为String类型的Set

python - 如何验证并知道一个 URL 是否是 Google Docs URL? | python , flask |

ssl - 通过 websocket 登录 - 这安全吗?

使用自定义 java 运行时镜像时发生 javax.net.ssl.SSLHandshakeException 但并非没有