11 Mayıs 2016 Çarşamba

HttpsURLConnection Sınıfı

constructor
Şöyle yaparız.
URL url = new URL(...);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
getDefaultHostNameVerifier metodu
Hangi alan adlarına güvenildiğini bulmak için şöyle yaparız.
// Open SSLSocket directly to gmail.com
SocketFactory sf = SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) sf.createSocket("gmail.com", 443);
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
SSLSession s = socket.getSession();

// Verify that the certicate hostname is for mail.google.com
// This is due to lack of SNI support in the current SSLSocket.
if (!hv.verify("mail.google.com", s)) {
    throw new SSLHandshakeException("Expected mail.google.com, "
                                    "found " + s.getPeerPrincipal());
}

// At this point SSLSocket performed certificate verificaiton and
// we have performed hostname verification, so it is safe to proceed.

// ... use socket ...
socket.close();
getInputStream metodu
Şöyle yaparız.
InputStream ins = con.getInputStream();
setDefaultHostNameVerifier metodu
Şöyle yaparız.
HostnameVerifier allHostsValid = new HostnameVerifier() {
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
};

HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
setDefaultSSLSocketFactory metodu
SSLContext sınıfı belirtiriz. Şöyle yaparız.
SSLContext sc = ...;
...
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());





Hiç yorum yok:

Yorum Gönder