How can I detect timeouts on reading data from a web server ?

D

Derek Clarkson

Hi all,
Our company uses a courier company to deliver our product to clients. So we
can monitor the status of deliveries I've written a program to screen scrap
the courier companies online tracking system (web based).

This works fine, except if I run it over night. when I come in the next
morning I typically find the program stopped, waiting for a reply from the
web server.

I'm guess that either their system, or our ISP has a temporary interuption
of service some time late at night and that causes either the program to
loose contact with the web server or the reply by the web server to be
"lost".

I would like to add a timeout to the code which reads data from the HTML
stream coming from the web server, but I can't see how to do it. I'm using
the following to get a reader with which I get data from the web server:

return new InputStreamReader(objURLConnect.getInputStream());

Any ideas ?
 
A

ak

I'm guess that either their system, or our ISP has a temporary interuption
of service some time late at night and that causes either the program to
loose contact with the web server or the reply by the web server to be
"lost".

Yes, ISP usually disconnect you once a day.
This is a BIG problem for java if you become no exception - because you
can't interrupt thread blocking on IO call.
It is better a) to speak with your ISP or b) search another ISP.
another way is to write some prog. which should observe your connection -
and just restart your java-scrap-programm.
I would like to add a timeout to the code which reads data from the HTML
stream coming from the web server, but I can't see how to do it.

you can't.
Setting timeout is only possible for ServerSockets.


The best way is to use nio package (only with java 1.4).


____________

http://reader.imagero.com the best java image reader.
 
M

Matt Humphrey

ak said:
Yes, ISP usually disconnect you once a day.
This is a BIG problem for java if you become no exception - because you
can't interrupt thread blocking on IO call.
It is better a) to speak with your ISP or b) search another ISP.
another way is to write some prog. which should observe your connection -
and just restart your java-scrap-programm.


you can't.
Setting timeout is only possible for ServerSockets.

..setSoTimeout works fine on client sockets. However, you may have to manage
the socket yourself as it may not be possible to get at the socket through
URLConnection. A good application should be able to manage periodic
network disruptions.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
B

Ben_

Yes, the OP could use Jakarta HTTPClient, instead of HTTPUrlConnection.
HTTPClient has a timeout parameter.
 
G

Gregory A. Swarthout

Derek Clarkson said:
Hi all,
Our company uses a courier company to deliver our product to clients. So we
can monitor the status of deliveries I've written a program to screen scrap
the courier companies online tracking system (web based).

This works fine, except if I run it over night. when I come in the next
morning I typically find the program stopped, waiting for a reply from the
web server.

I'm guess that either their system, or our ISP has a temporary interuption
of service some time late at night and that causes either the program to
loose contact with the web server or the reply by the web server to be
"lost".

I would like to add a timeout to the code which reads data from the HTML
stream coming from the web server, but I can't see how to do it. I'm using
the following to get a reader with which I get data from the web server:

return new InputStreamReader(objURLConnect.getInputStream());

Any ideas ?

If you use Sun's JDK you can use:

System.setProperty("sun.net.client.defaultConnectTimeout", "10000");

where 10000, or the number of your choice, is the number of milliseconds.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top