white_gecko
white_gecko

Reputation: 5086

ClassCastException with HttpsURLConnection

I try to establisch a HttpsURLConnection with:

HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection()

but I get an Exception:

E/JavaBinder(  901): java.lang.ClassCastException: org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection

But I can't find out, why. The same example is everywhere across the web.

Upvotes: 1

Views: 3000

Answers (2)

Cheryl Simon
Cheryl Simon

Reputation: 46844

The ClassCastException is telling you that the object being returned is not a HttpsUrlConnection. The cast you are doing is inherently unsafe, instead you should something like:

URLConnection conn = new URL(url).openConnection();
if (conn instanceof HttpsURLConnection) {
  // do stuff
}
else {
  // error?
}

As to the reason its not giving you an Https version, what url are you providing it with? My guess is you are giving it http:.. instead of https:...

Upvotes: 4

erickson
erickson

Reputation: 269637

What is the URL? It looks like you are using a plain "http:" scheme URL, but expecting an HTTPS connection.

Upvotes: 3

Related Questions