Creato da pastuweb.com
Share My Page
My Social Accounts
Account FaceBook Account LinkedIn Account Twitter Account Google Plus Account Git Hub
 
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
 
public class GetCall {
 
private static Logger log = Logger.getLogger("GetCall");
 
public static URL getURL(String endpoint) throws MalformedURLException {
return new URL(endpoint);
}
 
private static final String endpoint = "https://yourURL";
 
static public void main(String argv[]) throws Exception {
 
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
log.info("Warning: URL Host: " + urlHostName
+ " vs. " + session.getPeerHost());
return true;
}
};
 
trustAllHttpsCertificates();
HttpsURLConnection.setDefaultHostnameVerifier(hv);
 
sendAuthGet(getURL(endpoint));
}
 
public static void sendAuthGet(URL url) throws Exception {
 
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
 
int responseCode = con.getResponseCode();
log.info("\nSending 'GET' request to URL : " + url);
log.info("Response Code : " + responseCode);
 
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
 
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
 
//print result
log.info("HTTPUtil: Response sendGet - \n\n"+response.toString());
}
 
 
 
private static void trustAllHttpsCertificates() throws Exception {
 
// Create a trust manager that does not validate certificate chains
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, null);
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
 
}
 
public static class miTM implements javax.net.ssl.TrustManager,
javax.net.ssl.X509TrustManager {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
 
public boolean isServerTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
 
public boolean isClientTrusted(
java.security.cert.X509Certificate[] certs) {
return true;
}
 
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
 
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType)
throws java.security.cert.CertificateException {
return;
}
}
 
}