Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Test for unpinning
* Logs if HttpsURLConnection.setHostnameVerifier() is working or not.
* Also returns boolean to that effect.
static boolean testHostnameVerifier() {
boolean isWorking = false;
HttpsURLConnection connection = null;
try {
URL aUrl = new URL("");
// Get our HTTPS connection object
connection = (HttpsURLConnection) aUrl.openConnection();
// Register a hostname verifier which always fails
connection.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
Log.i("testHostnameVerifier", "CALLED");
return false;
// Should never get here with the HostnameVerifier we just set.
Log.i("testHostnameVerifier", "HostnameVerifer NOT WORKING");
} catch (SSLPeerUnverifiedException ex) {
// This exception happens with hostname verifier returns false
Log.i("testHostnameVerifier", "HostnameVerifer IS WORKING");
isWorking = true;
} catch(Exception ex) {
Log.e("testHostnameVerifier", ex.getMessage());
if(connection != null)
return isWorking;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment