Bonjour, je cherche à récupérer le contenu d'une page html à partir d'une url sécurisée.

J'ai trouvé ce code sur Internet car il semblait convenir pour ce que je voulais faire. Mais quand je l'exécute à partir de ma boite, il me met une erreur de type Connection timed out: connect

Merci de votre aide,
Julien

PS : j'ai bien recherché avant sur le forum et les tutoriels mais étant débutant, parfois je ne comprend pas bien les réponses.


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
public class TestURL {
	public static void main(String arg[]) {
		try {
			System.setProperty("proxySet", "true");
			System.setProperty("http.proxyHost","/*adresse de mon proxy*/");
			System.setProperty("http.proxyPort","8080");
			String furlsURL = "/*adresse sécurisé*/";
 
			URL url = new URL(furlsURL);
 
			String sAuth;
 
			// 1. Open the connection and keep it open in the instance of this
			// class
 
			HttpsURLConnection urlConn = (HttpsURLConnection) url
					.openConnection();
 
			// Init trust managers so the authenticity of the certificate is in
			// our control.
			X509TrustManager trustMgr = new MyTrustManager();
 
			TrustManager trustMgrs[] = { trustMgr };
 
			SSLContext ctx = SSLContext.getInstance("SSL");
			ctx.init(null, trustMgrs, null);
 
			SSLSocketFactory sf = ctx.getSocketFactory();
			urlConn.setSSLSocketFactory(sf);
 
			urlConn.setHostnameVerifier(new MyHostNameVerifier());
 
			urlConn.setDoInput(true);
			urlConn.setDoOutput(true);
			urlConn.setUseCaches(false);
 
			System.out.println("url: " + url);
 
			InputStream in = urlConn.getInputStream();
 
			int c = 0;
			while ((c = in.read()) != -1) {
				System.out.print((char) c);
			}
 
		} catch (Exception e) {
			System.out.println(e);
		} // end of try-catch
	}
}
 
class MyTrustManager implements X509TrustManager {
	MyTrustManager() { // constructor
	// create/load keystore
	// No need to load the keystore because it will be validated on demand.
	}
 
	public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
		return;
	}
 
	public java.security.cert.X509Certificate[] getAcceptedIssuers() {
		return null;
	}
 
	public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
		return;
	}
 
	public void checkClientTrusted(X509Certificate chain[], String authType)
			throws CertificateException {
		return;
	}
 
	public void checkServerTrusted(X509Certificate chain[], String authType)
			throws CertificateException {
		return;
	}
}
 
class MyHostNameVerifier implements HostnameVerifier {
	public boolean verify(String hostname, SSLSession session) {
		return true;
	}
}