Extending HttpURLConnection

Greetings,

I've been using HttpURLConnection to implement WebDAV on 1.3 and 1.4 JDK. I've now upgraded to 1.6 and am no longer able to cast from sun.net.www.protocol.http.HttpURLConnection to my extension.

My custom HttpURLConnection does extend the sun class and when I retrieve the HttpURLConnection from URL.openConnection, I receive a ClassCastException. Below is my code snippet:

mycustomclass.HttpURLConnection http = (mycustomclass.HttpURLConnection)url.openConnection();

http.setRequestProperty("Authorization","Basic " + auth.toString() );

http.setRequestProperty("Content-Type","text/xml" );

http.setDoOutput(true );

I've tested thaturl.openConnection(); does return a HttpURLConnection object, but the class cast exception is still thrown. The "Authorization" property is an encoded Base64 representation of my username and password.

I've been able to get around this by using the constructor in HttpURLConnection as below instead of casting from the openConnection:

mycustomclass.HttpURLConnection http =new mycustomclass.HttpURLConnection(url,"", 0);

However, now when I try to retrieve the output stream I recieve a java.net.ConnectionException:connect: Address is invalid on local machine, or port is not valid on remote machine.

The address is valid and the code works under 1.4JDK, but not on 1.6. I noticed that the HttpAuthenticator class is deprecated, which I've also extended. Any idea why this approach doesn't work on JDK 1.6?

Thanks,

Marcel

[1770 byte] By [MarcelSa] at [2007-11-27 9:48:08]
# 1
What does the exception say the actual class is?
ejpa at 2007-7-13 0:16:21 > top of Java-index,Core,Core APIs...
# 2
why did you give the class that extends HttpURLConnection the same name? That could ONLY lead to confusion.
RedUnderTheBeda at 2007-7-13 0:16:21 > top of Java-index,Core,Core APIs...
# 3

> cast from sun.net.www.protocol.http.HttpURLConnection to my extension

You don't have to do that, in fact you can't, you can only cast whatever is returned to your class.

It sounds as though you no longer have your protocol handler package installed correctly. The actual class reported in the exception will confirm that.

ejpa at 2007-7-13 0:16:21 > top of Java-index,Core,Core APIs...