java.lang.ClassCastException: cannot be cast to

Posted on 2012-04-12
Last Modified: 2012-04-17
Hi Experts,

i am facing a problem please help me, my problem, i have to connect another server, get response code and based on the response code redirect it to ther website or error page, my code was working fine for http site, now i need to do the same thing for HTTPS site but i am getting the below error :

i have seen the simillar question on this site but i followed the same still i am getting this error, actually i tried disabling the certificate verification by using "TrustManager" but still throwing the same error.

The error is thrown on this line :
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

i have attached the java file, please have a look and let me know the solution.
if you have a example code please provide me.

Error :
java.lang.ClassCastException: cannot be cast to
Question by:quickjava
  • 5
  • 4
LVL 35

Accepted Solution

mccarl earned 500 total points
ID: 37840508
The error is because '' does not extend from '', as the error message is implying.

Do you need to call any methods of ''? If not just cast the connection as '' instead (as that IS a superclass) and you should be right!

Author Comment

ID: 37840536

I tried this

Intead of  this : URL url = new URL(fURL);

i tried with this : wsURL = new URL(null, fURL,new;

and now i am getting this  error : No X509TrustManager implementation available

Please help me
LVL 35

Expert Comment

ID: 37840571
Did you try what I mentioned above?

For that second error message, did you get that at the ... new URL(...) line or does it throw that error later when you are actually trying to use the connection?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Closing Comment

ID: 37843636
Thanks it is working perfectly  with these two combination : url = new URL(null, fURL,new;  connection = (HttpsURLConnection) url.openConnection();
LVL 35

Expert Comment

ID: 37845295
Glad to hear it is working! Cheers

Author Comment

ID: 37852573
Hi, i am facing some other problem, it is working when i just pass "",  but when i pass the query parameters like below it is not working, tried with encoding the URL and got "MalformedURLException" exception, if i try without encodeingURL it just trys for long time and gives connection timeout error, i am not sure but thinking it may be because of SSL cetificate, if it is SSL related could you please let me know the steps to add the SSL in my server. jNRdDhYImgco

LVL 35

Expert Comment

ID: 37854069
I would doubt that it is SSL related because as far as I know the certificate applies to the site as a whole, so if it works without the query parameters, I don't think that would be it.

Is this a site that you normally have to login to access these pages? Maybe that SessionGUID is no longer valid by the time you are trying to access it.

Actually, looking it again, you say that this is your server? You should be able to debug on the server side why the connection times out?

Author Comment

ID: 37858673
Thanks for your reply, tried checking it in the log but is not showing any error except timeout.
i am also thinking the same, the SessionID is getting invalide when i connect to the server,  i am not hunderstanding how to maintain valid sessionID.

if there is anyway of maintaining the valid sessionID please let me know.
LVL 35

Expert Comment

ID: 37859191
I asked this previously....

> Is this a site that you normally have to login to access these pages?

If so, then you may have to get your code to follow the same login procedure in order to get a valid SessionID.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android development question 2 75
login form jsp example 2 49
Adding multiple JVM environments to RedHat 6 7 69
java example issue 5 42
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question