?
Solved

Using HttpsURLConnection

Posted on 2003-02-28
25
Medium Priority
?
1,284 Views
Last Modified: 2008-01-09
I'm having a problem being able to get an HttpsURLConnection for a secure URL.

In the following case, I get a ClassCastException:

URL aURL = new URL("https://www.verisign.com");
HttpsURLConnection httpsURLConn = (HttpsURLConnection)aURL.openConnection();

Upon further investigation, I tried the following:

URL aURL = new URL("https://www.verisign.com");
URLConnection urlConn = aURL.openConnection();

if (urlConn instanceof com.sun.net.ssl.HttpsURLConnection) {
System.out.println("*** openConnection returns an instanceof com.sun.net.ssl.HttpsURLConnection");
}
if (urlConn instanceof javax.net.ssl.HttpsURLConnection) {
System.out.println("*** openConnection returns an instanceof javax.net.ssl.HttpsURLConnection");
}
if (urlConn instanceof HttpURLConnection) {
System.out.println("*** openConnection returns an instnace of HttpURLConnection");
}

And found that my output is:
*** openConnection returns an instanceof com.sun.net.ssl.HttpsURLConnection
*** openConnection returns an instnace of HttpURLConnection

Doesn't that seem like a mistake? Why is URL.openConnection() returning a deprecated class?

Also, what do I need to do so that I can get an HttpsURLConnection?  Do I need to use Sockets or can I use URLConnection?
0
Comment
Question by:aagha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 11
25 Comments
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8041320
Pls try following:

URL aURL = new URL("https://www.verisign.com");
HttpURLConnection httpsURLConn = (HttpURLConnection)aURL.openConnection();

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8041336
>>Doesn't that seem like a mistake?

No, as com.sun.net.ssl.HttpsURLConnection
extends java.net.HttpURLConnection.

>.Also, what do I need to do so that I can get an HttpsURLConnection?  

Cast the result from URLConnection connect in the normal way
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8041348
Your code:

>>
URL aURL = new URL("https://www.verisign.com");
HttpsURLConnection httpsURLConn = (HttpsURLConnection)aURL.openConnection();
>>

Seems perfectly OK. What was the problem with this?
0
Technology Partners: 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 Comment

by:aagha
ID: 8041531
Umangjoshi -- You solution gives me a HttpURLConnection, what I need is a HttpsURLConnection.

CEHJ -- The problem is at runtime.  This line:

HttpsURLConnection httpsURLConn = (HttpsURLConnection)aURL.openConnection();

throws a ClassCastException:

*** openConnection returns a urlc instanceof com.sun.net.ssl.HttpsURLConnection
*** openConnection returns a HttpURLConnection
com.greece101.xmlservice.XMLServiceException: java.lang.ClassCastException

I think this is happening because the type returned by URL.openConnection is an HttpURLConnection (and more specifically, com.sun.net.ssl.HttpsURLConnection -- which is deprecated -- I'm still not understnding this: why is openConnection returning a _deprecated_ object?!) and I'm trying to cast that to a HttpsURLConnection.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8041554
OK - i'll ponder this for a minute. While i'm doing that - what makes you think that com.sun.net.ssl.HttpsURLConnection is 'deprecated'?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8041578
Please post the *exact* code you're using.
0
 

Author Comment

by:aagha
ID: 8041581
I should make clear that I'm using JDK1.4, and not the seperate JSSE jars.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8041592
OK - but the exact code please.
0
 

Author Comment

by:aagha
ID: 8041622
I think its deprecated because when I compile, i get the following:

warning: com.sun.net.ssl.HttpsURLConnection in com.sun.net.ssl has been deprecated

HttpsURLConnection is now in javax.net.ssl.

My code is as follows:

--- begin code ---
URL aURL = new URL("https://www.verisign.com");
URLConnection urlConn = aURL.openConnection();

if (urlConn instanceof com.sun.net.ssl.HttpsURLConnection) {
    System.out.println("*** openConnection returns a urlc instanceof com.sun.net.ssl.HttpsURLConnection");
}                    
if (urlConn instanceof javax.net.ssl.HttpsURLConnection) {
    System.out.println("*** openConnection returns a urlc instanceof javax.net.ssl.HttpsURLConnection");
}          
if (urlConn instanceof HttpURLConnection) {
    System.out.println("*** openConnection returns a HttpURLConnection");
}

aHttpURLConnection = (HttpsURLConnection)urlConn;
System.out.println("*** after cast");
--- end code ---

The code does _not_ get to "*** after cast"; I get the ClassCastException at this point.
0
 

Author Comment

by:aagha
ID: 8041639
Sorry, forgot the following line at top of the above code block:

HttpsURLConnection aHttpURLConnection = null;
0
 

Author Comment

by:aagha
ID: 8041671
Sorry, forgot the following line at top of the above code block:

HttpsURLConnection aHttpURLConnection = null;
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 225 total points
ID: 8041678
OK, you should have something more like this:

javax.net.ssl.HttpsURLConnection newHttps = null;
com.sun.net.ssl.HttpsURLConnection oldHttps = null;
boolean usingOld = false;

URL aURL = new URL("https://www.verisign.com");
URLConnection urlConn = aURL.openConnection();

if (urlConn instance of com.sun.net.ssl.HttpsURLConnection) {
  usingOld = true;
  oldHttps = (com.sun.net.ssl.HttpsURLConnection)urlConn;
  System.out.println("Got implementation");
}

You should also implement the 'new' stuff in the same way.



0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8041709
>>I should make clear that I'm using JDK1.4, and not the seperate JSSE jars.

I don't have that JDK at the moment, but i *do* have that runtime. Usually the runtime rt.jar is very little different from the JDK rt.jar. The Https classes are not in rt.jar on my machine, but are in jsse.jar
0
 

Author Comment

by:aagha
ID: 8041737
CEHJ -- While I understand your solution, I don't think that urlConn will _ever_ be an instance of javax.net.ssl.HttpsURLConnection.  Can you think of a reason that it ever might be?

Therefore, the "new" if statement would never get executed.

Just out of curiosity, what are your thoughts on using the deprecated HttpsURLConnection versus the new one?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8041773
>>Just out of curiosity, what are your thoughts on using the deprecated HttpsURLConnection versus the new one?

The point is, it's up to the implementation of the library what openConnection returns. I don't have the source here to inspect (if it exists for Https). You simply have to work with the result.

Incidentally what method(s) are you going to call on that connection that is/are not in the superclass?
0
 

Author Comment

by:aagha
ID: 8041855
Thanks for the insight.

I need to call setHostNameVerfier, which exists only HttpsURLConnection.  Thus the importance of being able to reference that specific class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8041889
OK. That's my interpretation of what's happening. My *guess* is that there's been some disorganization over at Sun, hence the 'deprecated' business, despite the fact they're returning deprecated instances!
0
 

Author Comment

by:aagha
ID: 8041955
Thx for all the help and discussion.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8041990
:-)
0
 

Author Comment

by:aagha
ID: 8052457
CEHJ -- Just in case you're interested, I found the solution to this problem:

http://forum.java.sun.com/thread.jsp?thread=196586&forum=2&message=648727
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8052706
Interesting, although

>>
HttpsURLConnection migrated to javax.net.ssl
from com.sun.net.ssl for 1.4.
>>

seems to directly contradict your experience, since it's an instance of com.sun.net.ssl.HttpsURLConnection you're getting with 1.4. Did setting that system property mentioned help?
0
 

Author Comment

by:aagha
ID: 8056319
Actually, the problem was that the sysem property:

-Djava.protocol.handler.pkgs=com.sun.net.ssl.internal.www.protocol

_was_ set (I had thought it was required).  I removed it, allowing for the use of javax.net.ssl.HttpsURLConnection rather than com.sun.net.ssl.HttpsURLConnection.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057014
Ah i see. You set that on the command line did you?
0
 

Author Comment

by:aagha
ID: 8057126
Yup.  I read about JSSE, and thought it was required.  The docs for javax.net.ssl.HttpsURLConnection don't mention that its not required.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057143
I'm surprised you didn't guess what the problem was in that case ;-)
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

770 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