gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:08:33 25 4
gpt4 key购买 nike

我有一台 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com