• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to wait for an HTTP response code, and auto retry?

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm writing an app that has to connect to a server. It needs to wait up to 60 seconds for a code 200, and if it doesn't get a code 200 in that time then try again.

At the moment, my code is:


But how can I specify a waiting period of 60 seconds for the 200 code to arrive.
And is there some way of making the retry automatic, or must I write a load more code for it?

Many thanks,
James
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are going to try to get around network congestion by. . . creating more congestion?
Anyway, to specify a timeout, you could use the system network properties:

sun.net.client.defaultConnectTimeout (default: -1)
sun.net.client.defaultReadTimeout (default: -1)
These properties specify the default connect and read timeout (resp.) for the protocol handler used by java.net.URLConnection.

sun.net.client.defaultConnectTimeout specifies the timeout (in milliseconds) to establish the connection to the host. For example for http connections it is the timeout when establishing the connection to the http server. For ftp connection it is the timeout when establishing the connection to ftp servers.

sun.net.client.defaultReadTimeout specifies the timeout (in milliseconds) when reading from input stream when a connection is established to a resource.
Java 1.4.2 SDK - Networking Properties
Or use the
Jakarta Commons HttpClient class, which specifies a timeout method.
You will have to write the logic to perform retries.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!