I'm reading a web page using HttpClient like this:
httpclient = new DefaultHttpClient();
httpget = new HttpGet("http://google.com");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream PIS = entity.getContent();
}
I need a timeout on the entire job (connecting, waiting & reading - All together or separately).
I tried setting timeout parameters just after httpclient = new DefaultHttpClient();
line:
int timeout=10;
httpclient.getParams().setParameter("http.socket.timeout", timeout * 1000);
httpclient.getParams().setParameter("http.connection.timeout", timeout * 1000);
httpclient.getParams().setParameter("http.connection-manager.timeout", new Long(timeout * 1000));
httpclient.getParams().setParameter("http.protocol.head-body-timeout", timeout * 1000);
But it didn't worked (It timeouts after about 10 times more than the timeout I set).
So I tried a thread to cancel request after a time using httpget.abort()
& httpclient.getConnectionManager().shutdown()
just after httpget = new HttpGet("http://google.com");
line like this:
(new Timer()).schedule(new java.util.TimerTask() {
public void run() {
httpget.abort();
httpclient.getConnectionManager().shutdown();
}
},10000);
but it had no effect(Timer runs; but those two lines of code do nothing!)!!
I also tried to use this:
URL url = new URL("http://google.com");
URLConnection con = url.openConnection();
con.setConnectTimeout(10000);
con.setReadTimeout(10000);
InputStream PIS = con.getInputStream();
but it was same as my first try (setting timeout parameters in HttpClient
)!!
what is the problem?
How can I solve my timeout problem?
Thanks