与Java访问HTTPS URL相同,然后访问HTTP URL。您可以随时使用
URL url = new URL("https://hostname:port/file.txt");URLConnection connection = url.openConnection();InputStream is = connection.getInputStream();// .. then download the file但是,当无法验证服务器的证书链时,您可能会遇到一些问题。因此,出于测试目的,您可能需要禁用证书验证并信任所有证书。
为此,请写:
// Create a new trust manager that trust all certificatesTrustManager[] trustAllCerts = new TrustManager[]{ new 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) { } }};// Activate the new trust managertry { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());} catch (Exception e) {}// And as before now you can use URL and URLConnectionURL url = new URL("https://hostname:port/file.txt");URLConnection connection = url.openConnection();InputStream is = connection.getInputStream();// .. then download the file


