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.o penConnect ion();
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.HttpsURLCo nnection) {
System.out.println("*** openConnection returns an instanceof com.sun.net.ssl.HttpsURLCo nnection") ;
}
if (urlConn instanceof javax.net.ssl.HttpsURLConn ection) {
System.out.println("*** openConnection returns an instanceof javax.net.ssl.HttpsURLConn ection");
}
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.HttpsURLCo nnection
*** 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?
In the following case, I get a ClassCastException:
URL aURL = new URL("https://www.verisign.com");
HttpsURLConnection httpsURLConn = (HttpsURLConnection)aURL.o
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.HttpsURLCo
System.out.println("*** openConnection returns an instanceof com.sun.net.ssl.HttpsURLCo
}
if (urlConn instanceof javax.net.ssl.HttpsURLConn
System.out.println("*** openConnection returns an instanceof javax.net.ssl.HttpsURLConn
}
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.HttpsURLCo
*** 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?
>>Doesn't that seem like a mistake?
No, as com.sun.net.ssl.HttpsURLCo nnection
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
No, as com.sun.net.ssl.HttpsURLCo
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.o penConnect ion();
>>
Seems perfectly OK. What was the problem with this?
>>
URL aURL = new URL("https://www.verisign.com");
HttpsURLConnection httpsURLConn = (HttpsURLConnection)aURL.o
>>
Seems perfectly OK. What was the problem with this?
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.o penConnect ion();
throws a ClassCastException:
*** openConnection returns a urlc instanceof com.sun.net.ssl.HttpsURLCo nnection
*** openConnection returns a HttpURLConnection
com.greece101.xmlservice.X MLServiceE xception: java.lang.ClassCastExcepti on
I think this is happening because the type returned by URL.openConnection is an HttpURLConnection (and more specifically, com.sun.net.ssl.HttpsURLCo nnection -- 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.
CEHJ -- The problem is at runtime. This line:
HttpsURLConnection httpsURLConn = (HttpsURLConnection)aURL.o
throws a ClassCastException:
*** openConnection returns a urlc instanceof com.sun.net.ssl.HttpsURLCo
*** openConnection returns a HttpURLConnection
com.greece101.xmlservice.X
I think this is happening because the type returned by URL.openConnection is an HttpURLConnection (and more specifically, com.sun.net.ssl.HttpsURLCo
OK - i'll ponder this for a minute. While i'm doing that - what makes you think that com.sun.net.ssl.HttpsURLCo nnection is 'deprecated'?
Please post the *exact* code you're using.
ASKER
I should make clear that I'm using JDK1.4, and not the seperate JSSE jars.
OK - but the exact code please.
ASKER
I think its deprecated because when I compile, i get the following:
warning: com.sun.net.ssl.HttpsURLCo nnection 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.HttpsURLCo nnection) {
System.out.println("*** openConnection returns a urlc instanceof com.sun.net.ssl.HttpsURLCo nnection") ;
}
if (urlConn instanceof javax.net.ssl.HttpsURLConn ection) {
System.out.println("*** openConnection returns a urlc instanceof javax.net.ssl.HttpsURLConn ection");
}
if (urlConn instanceof HttpURLConnection) {
System.out.println("*** openConnection returns a HttpURLConnection");
}
aHttpURLConnection = (HttpsURLConnection)urlCon n;
System.out.println("*** after cast");
--- end code ---
The code does _not_ get to "*** after cast"; I get the ClassCastException at this point.
warning: com.sun.net.ssl.HttpsURLCo
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.HttpsURLCo
System.out.println("*** openConnection returns a urlc instanceof com.sun.net.ssl.HttpsURLCo
}
if (urlConn instanceof javax.net.ssl.HttpsURLConn
System.out.println("*** openConnection returns a urlc instanceof javax.net.ssl.HttpsURLConn
}
if (urlConn instanceof HttpURLConnection) {
System.out.println("*** openConnection returns a HttpURLConnection");
}
aHttpURLConnection = (HttpsURLConnection)urlCon
System.out.println("*** after cast");
--- end code ---
The code does _not_ get to "*** after cast"; I get the ClassCastException at this point.
ASKER
Sorry, forgot the following line at top of the above code block:
HttpsURLConnection aHttpURLConnection = null;
HttpsURLConnection aHttpURLConnection = null;
ASKER
Sorry, forgot the following line at top of the above code block:
HttpsURLConnection aHttpURLConnection = null;
HttpsURLConnection aHttpURLConnection = null;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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
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
ASKER
CEHJ -- While I understand your solution, I don't think that urlConn will _ever_ be an instance of javax.net.ssl.HttpsURLConn ection. 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?
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?
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?
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.
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!
ASKER
Thx for all the help and discussion.
:-)
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
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.HttpsURLCo nnection you're getting with 1.4. Did setting that system property mentioned help?
>>
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.HttpsURLCo
ASKER
Actually, the problem was that the sysem property:
-Djava.protocol.handler.pk gs=com.sun .net.ssl.i nternal.www.protocol
_was_ set (I had thought it was required). I removed it, allowing for the use of javax.net.ssl.HttpsURLConn ection rather than com.sun.net.ssl.HttpsURLCo nnection.
-Djava.protocol.handler.pk
_was_ set (I had thought it was required). I removed it, allowing for the use of javax.net.ssl.HttpsURLConn
Ah i see. You set that on the command line did you?
ASKER
Yup. I read about JSSE, and thought it was required. The docs for javax.net.ssl.HttpsURLConn ection don't mention that its not required.
I'm surprised you didn't guess what the problem was in that case ;-)
URL aURL = new URL("https://www.verisign.com");
HttpURLConnection httpsURLConn = (HttpURLConnection)aURL.op