1

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.

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

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?
I'm not sure, you probably want to start a new question for that.
I think, what I want is not possible :-S
I too get this exception, but I am using https, any idea what might be going wrong.
3

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

4 Comments

Hi erickson , i am facing the same problem with https url, can you suggest something.
@KartikShah Post a new question and provide detail of your situation. Without that, I can only assume that you are mistaken, and that you are accessing a resource with HTTP.
@erickson the scenario is same, i think the issue is with the server, because it has multiple nodes and and some configuration is going wrong on some node. I am saying this because the same code works sometimes and doesnt the other time, i will post a new question once i am sure its not a server issue. Thanks anyways.
@KartikShah Perhaps the server is erroneously redirecting to HTTP some times. Is there a load balancer or cluster involved?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.