This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Fuad Abinader
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Dunn
slicker
Ranch Hand
Posts: 1108
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()?
 
Fuad Abinader
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic