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
|
public static boolean TestUserConnection(String urlToGet, String login,
String password) throws Exception {
boolean result = false;
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
System.setProperty("http.maxRedirects", "2");
System.setProperty("https.maxRedirects", "2");
System.setProperty("http.keepAlive ", "false");
System.setProperty("https.keepAlive ", "false");
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
URL wsURL = new URL(urlToGet);
HttpsURLConnection myConnect = (HttpsURLConnection)wsURL.openConnection();
myConnect.setDefaultSSLSocketFactory(sc.getSocketFactory());
Authenticator.setDefault(new MyAuthenticator(login,password));
BufferedReader in = null;
try{
URL url = new URL(urlToGet);
in = new BufferedReader(new InputStreamReader(url.openStream()));
if ((in.readLine()) != null) {
// System.out.println(str);
}
result = true;
}
catch (Exception e) {
// Affiche la cause de l'exception
}
finally {
// Fermeture silencieuse
if (in != null) {
try {
in.close();
catch(IOException e) {
}
in = null;
}
myConnect.disconnect();
}
return result;
} |
Partager