I'm creating a class that handles HTTP connections, and I want to handle both HTTP and HTTPS but using the same variable (so I can just use the same code to send data, etc.) Currently, my code looks something like this:
if (ssl)
{
conn = (HttpsURLConnection) new URL(...).openConnection();
conn.setHostnameVerifier(...);
}
else
{
conn = (HttpURLConnection) new URL(...).openConnection();
}
When "conn" is of type HttpsURLConnection, the HttpURLConnection cast fails. When "conn" is of type HttpURLConnection or URLConnection, the "setHostnameVerifier" and other HTTPS related methods are inaccessible.
Given that HttpsURLConnection is a subclass of the HttpURLConnection class, I'd have thought casting it would have worked, but I'm obviously mistaken. Is there any way of making this code work so that I can access HTTPS methods when I need them?