tags:

views:

22

answers:

2

Hello, 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.

+1  A: 

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

erickson
+1  A: 

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:...

Mayra
That's right, also what @erickson says. I want to get a connection to a http:// url but want to send a client certificate (as in X.509) to this server for authentication, so I thought that I need a https connection. Do you maybe know how to provide a certificate with an http connection?
white_gecko
I'm not sure, you probably want to start a new question for that.
Mayra
I think, what I want is not possible :-S
white_gecko