Note that there are some explanatory texts on larger screens.

plurals
  1. POStrict and reliable timeout on HttpClient
    text
    copied!<p>I'm reading a web page using HttpClient like this: </p> <pre><code> 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(); } </code></pre> <p>I need a timeout on the entire job (connecting, waiting &amp; reading - All together or separately).<br> I tried setting timeout parameters just after <code>httpclient = new DefaultHttpClient();</code> line: </p> <pre><code> 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); </code></pre> <p>But it didn't worked (It timeouts after about 10 times more than the timeout I set).<br> So I tried a thread to cancel request after a time using <code>httpget.abort()</code> &amp; <code>httpclient.getConnectionManager().shutdown()</code> just after <code>httpget = new HttpGet("http://google.com");</code> line like this:</p> <pre><code> (new Timer()).schedule(new java.util.TimerTask() { public void run() { httpget.abort(); httpclient.getConnectionManager().shutdown(); } },10000); </code></pre> <p>but it had no effect(Timer runs; but those two lines of code do nothing!)!!<br> I also tried to use this: </p> <pre><code>URL url = new URL("http://google.com"); URLConnection con = url.openConnection(); con.setConnectTimeout(10000); con.setReadTimeout(10000); InputStream PIS = con.getInputStream(); </code></pre> <p>but it was same as my first try (setting timeout parameters in <code>HttpClient</code>)!! </p> <p>what is the problem?<br> <strong>How can I solve my timeout problem?</strong> </p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload