I am implementing a hostname verifier for my SSL client. I know that the Default Hostname Verifier of the HTTPS URL connection is static. My problem is that I do not want the hostname verification to be static because I have multiple threads in my client accessing this hostname verifier at the same time.
At the moment my code looks like this,:
I have an inner class for the hostname verification,
public class CreateHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String hostname, SSLSession session) {
try {
if (mInstance.getHost().equals(InetAddress.getByName(hostname)))
return true;
else if (hostname.equals(mInstance.getHost().getHostAddress()))
return true;
else if (hostname.equals(mInstance.getHost().getCanonicalHostName()))
return true;
else
return false;
} catch (UnknownHostException e) {
System.out.println("Unknown Host");
}
return false;
}
}
}
I call this class when I am building my endpoint,
address = mInstance.getSecureConnectionEndpoint();
try {
HttpsURLConnection Connection = (HttpsURLConnection) address.openConnection();
CreateHostnameVerifier hv = new CreateHostnameVerifier();
Connection.setDefaultHostnameVerifier(hv);
} catch (IOException e1) {
e1.printStackTrace();
}
Because I use setDefaultHostnameVerifer
and try to access it in a non static way by doing Connection.setDefaultHostnameVerifier
I get warnings like:
The static method setDefaultHostnameVerifier(HostnameVerifier) from the type HttpsURLConnection should be accessed in a static way
But this works absolutely fine, but in case I try to access it in a non-static way:
Connection.setHostnameVerifier(hv);
It does not work.
Can someone explain me the difference between the two, i.e., setHostnameVerifer
and setDefaultHostnameVerifier
.