Disable SSLHandshakeException for a single connection

https, java, security, ssl

Solution

Just use instance methods `setX()` instead of static `setDefaultX()`:

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(...);
connection.setHostnameVerifier(...);

Problem

I'm looking for a solution similar to this answer, but much safer. I'd like to disable the certificate validation, but for a single request only (which is all I need). So it should do one or more of the following - return to the secure state when the one request is done - disable validation for the given URL only - (maybe) use the insecure settings just for one thread Addendum I really wonder what's wrong with this question (score -2) when compared to the original one (score +46), when I'm asking for a more secure solution. Could someone explain? To explain why I need this: There's a valid server certificate and normally, it gets used. But I need to send one request to `localhost` and it has to work on a developer machine, too. It must be `https` as there's no `http` support.

Original source

Related problems