File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes How to know when a HttpURLConnection is on the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How to know when a HttpURLConnection is on the "connected" state?" Watch "How to know when a HttpURLConnection is on the "connected" state?" New topic
Author

How to know when a HttpURLConnection is on the "connected" state?

Fuad Abinader
Greenhorn

Joined: Feb 16, 2004
Posts: 8
Hi all,

I'm working on a university project to create a J2ME emulator, using J2SE API. Therefore, in order to allow HTTP connections to J2ME applications, I'm using a from the J2SE API. However I'm stucked with it, because I have to check if the instance is in the connected state, but there is no method available to return it. Does anyone knows if there is a way to know that?

Thanks in advance,

Fuad Abinader
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I don't see anything in the JavaDoc to help, but it does say that connect() is ok if it's already connected. If you just wanted to say:

see if it's ok to just always connect.

I suppose you could extend HttpURLConnection and add a method to return the protected "connected" variable, but I don't think I like that much.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
John Dunn
slicker
Ranch Hand

Joined: Jan 30, 2003
Posts: 1108
Http - isn't it ask & receive?

So you ask; and either you get, or you timeout. Right?

I use HttpURLConnection. I set one up at start up and use it over again all day long. Only gives a problem if the remote server that I'm trying to connect to goes down, or was never up.

Why do you need to check on connected()?


"No one appreciates the very special genius of your conversation as the dog does."
Fuad Abinader
Greenhorn

Joined: Feb 16, 2004
Posts: 8
Hi all,

Well, the problem is that I'm trying to create a J2ME emulator, and for that, I'm using J2SE API to provide J2ME services in the way the J2ME API specifies. For that, I'm making a wrapper for HTTP, in a way that I use a J2SE HttpURLConnection to simulate the behavior of a J2ME HttpConnection. The problem is that they have different behavior regarding methods that cause the transition to the connected state, and also they behave differently on when to return exceptions.
For example, in J2ME, if I open a HTTP connection, and call connection.getDate(), this causes the connection to go to the connected state; therefore, if I call the method connection.setRequestProperty(), this method should throw an IOException, because the connection will be at the connected state. If I do the same on a instance of J2SE HttpURLConnection, no exception is thrown. Therefore I have to check if the connection is in the connected state in order to return the IOException.
Can anyone help me on this issue? Any hints?

Regards,

Fuad Abinader
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ah, a much more interesting problem for sure! Just for grins, see if you can "cheat" and use reflection to change the visibility of the protected "connected" boolean. See if it even has the right value at the right time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to know when a HttpURLConnection is on the "connected" state?
 
Similar Threads
BufferedReader hangs my app?
HttpConnection returns null
J2ME ?
difference between openConnection() and connect() methods
Generating random values for colours