HTTPS er lidt mere tricky.
Jeg lavede engang dette eksempel:
import java.net.*;
import java.io.*;
import java.security.*;
import java.security.cert.*;
import javax.net.ssl.*;
public class HttpsGet {
public static void main(String[] args) {
try {
SSLContext sslctx = SSLContext.getInstance("SSL");
sslctx.init(null, new X509TrustManager[] { new MyTrustManager() }, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslctx.getSocketFactory());
//HttpsURLConnection.setDefaultHostnameVerifier(new MyHostnameVerifier());
URL url = new URL("
https://www.xxxx.dk/");
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
if (con.getResponseCode() == HttpsURLConnection.HTTP_OK) {
InputStream is = con.getInputStream();
OutputStream os = new FileOutputStream("C:\\z.z");
byte[] b = new byte[1000];
int n;
while ((n = is.read(b)) >= 0) {
os.write(b, 0, n);
}
os.close();
is.close();
}
con.disconnect();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyTrustManager implements X509TrustManager
{
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
//class MyHostnameVerifier implements HostnameVerifier {
// public boolean verify(String urlHostName, SSLSession session) {
// return true;
// }
//}
Det kræver java 1.4.x eller nyere