Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

how to use HttpURLConnection

Thread view: 
Ben - 10 Apr 2006 16:35 GMT
I need to use the HttpURLConnection to get the status code of a connection.

Since the class is abstract I need to implement usingProxy() and
disconnect(). I'm a new programmer and I have no idea how to implement
those two methods.

Could I get any pointer?

thanks,
Ben
Ben - 10 Apr 2006 17:00 GMT
> I need to use the HttpURLConnection to get the status code of a connection.
>
[quoted text clipped - 6 lines]
> thanks,
> Ben

Another problem to add:

it's complaining about not having a constructor for HttpURLConnection in
my class that extends it, yet when I create a constructor it complains
that it doesn't have a return type...

Can a constructor even have a return type?
Thomas Fritsch - 10 Apr 2006 17:33 GMT
> I need to use the HttpURLConnection to get the status code of a
> connection.
>
> Since the class is abstract I need to implement usingProxy() and
> disconnect().
No, you do *not* need to implement these methods.
When you open a connection to an URL (http://...), probably by
 url.openConnection();
you do *not* get an object of the abstract java.net.HttpURLConnection class
(the one whose documentation you've read) as such.
Instead you get an object of some non-abstract *subclass* of
java.net.HttpURLConnection, may be an object of class
sun.net.www.protocol.http.IDontKnowWhatConnection. Exactly what class you
get, you don't know and you don't need to know. Because this class is a
non-abstract class, it *has* implementations for all methods declared in
java.net.HttpURLConnection, including usingProxy() and disconnect().
Actually the non-abstract class will override *many* methods of
java.net.HttpURLConnection, not only the abstract ones.
 
> I'm a new programmer and I have no idea how to implement
> those two methods.
The key is to understand polymorphism. That means, when you have a method
 SomeObject someMethod();
then you do *not* necessarily get an object of SomeObjecta s such, but you
may get an object of any subclass of SomeObject. Consider for example:
 SomeObject someMethod() {
   return new SomeSpecializedObject(...);
 }
with the class implementation
 class SomeSpecializedObject extends SomeObject {
   ...
 }

Signature

"Thomas:Fritsch$ops.de".replace(':', '.').replace('$', '@')

Filip Larsen - 10 Apr 2006 17:38 GMT
> I need to use the HttpURLConnection to get the status code of a connection.

URL url = new URL("http://host/resource");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
int response = con.getResponseCode();

> Since the class is abstract I need to implement usingProxy() and
> disconnect().

No you don't need to do that. What you do seem to need however is to
read up on how to use the Java API. There are plenty of good books out
there and you can even get some of it for free, like the Sun Java
tutorials:

http://java.sun.com/docs/books/tutorial/index.html
http://java.sun.com/docs/books/tutorial/networking/urls/index.html

and of course the Java API documentation itself

http://java.sun.com/j2se/1.5.0/docs/api/ or
http://java.sun.com/j2se/1.4.2/docs/api/

Regards,
Signature

Filip Larsen

Ben - 10 Apr 2006 20:15 GMT
Thanks for the help, it makes things a whole lot easier!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.