Link to home
Start Free TrialLog in
Avatar of aagha
aagha

asked on

Using HttpsURLConnection

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?
Avatar of umangjoshi
umangjoshi

Pls try following:

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

Avatar of CEHJ
>>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
Your code:

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

Seems perfectly OK. What was the problem with this?
Avatar of aagha

ASKER

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.
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'?
Please post the *exact* code you're using.
Avatar of aagha

ASKER

I should make clear that I'm using JDK1.4, and not the seperate JSSE jars.
OK - but the exact code please.
Avatar of aagha

ASKER

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.
Avatar of aagha

ASKER

Sorry, forgot the following line at top of the above code block:

HttpsURLConnection aHttpURLConnection = null;
Avatar of aagha

ASKER

Sorry, forgot the following line at top of the above code block:

HttpsURLConnection aHttpURLConnection = null;
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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
Avatar of aagha

ASKER

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?
>>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?
Avatar of aagha

ASKER

Thanks for the insight.

I need to call setHostNameVerfier, which exists only HttpsURLConnection.  Thus the importance of being able to reference that specific class.
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!
Avatar of aagha

ASKER

Thx for all the help and discussion.
:-)
Avatar of aagha

ASKER

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
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?
Avatar of aagha

ASKER

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.
Ah i see. You set that on the command line did you?
Avatar of aagha

ASKER

Yup.  I read about JSSE, and thought it was required.  The docs for javax.net.ssl.HttpsURLConnection don't mention that its not required.
I'm surprised you didn't guess what the problem was in that case ;-)