ClassCastException with HttpsURLConnection

I am trying to establish an HttpsURLConnection connection 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 understand why. The same example is everywhere on the Internet.

+1
source share
2 answers

ClassCastException tells you that the returned object is not an HttpsUrlConnection. The cast you are doing is inherently unsafe, instead you need something like:

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

Due to the fact that it does not give you the Https version, what url do you provide? I assume that you give it http: .. instead of https: ...

+4
source

What is the url? It looks like you are using the simple http: URL, but expect an HTTPS connection.

+3
source

All Articles