java - Timeout in DefaultHttpClient -
i'm little confused on how timeouts in defaulthttpclient work.
i'm using code:
private defaulthttpclient createhttpclient() { httpparams my_httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(my_httpparams, 3000); httpconnectionparams.setsotimeout(my_httpparams, 15000); schemeregistry registry = new schemeregistry(); registry.register(new scheme("http", plainsocketfactory.getsocketfactory(), 80)); threadsafeclientconnmanager multithreadedconnectionmanager = new threadsafeclientconnmanager(my_httpparams, registry); defaulthttpclient httpclient = new defaulthttpclient(multithreadedconnectionmanager, my_httpparams); return httpclient; }
.
string url = "http://www.example.com"; defaulthttpclient httpclient = createhttpclient(); httpget httpget = new httpget(url); try { httpresponse response = httpclient.execute(httpget); statusline statusline = response.getstatusline(); mstatuscode = statusline.getstatuscode(); if (mstatuscode == 200){ content = entityutils.tostring(response.getentity()); } } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (illegalstateexception e){ e.printstacktrace(); }
when 15 seconds have passed , not data has been received, exception thrown, right? on method? thought .execute(httpget)
method 1 tells me throws clientprotocolexception
, ioexception
. me clearifying this?
it throw exception on execute()
. parent of sockettimeoutexception
ioexception
. catch block handling ioexception
able catch both.
try executing code.
httpparams my_httpparams = new basichttpparams(); httpconnectionparams.setconnectiontimeout(my_httpparams, 3000); httpconnectionparams.setsotimeout(my_httpparams, 1); defaulthttpclient defaulthttpclient = new defaulthttpclient(my_httpparams); httpget httpget = new httpget("http://google.com"); defaulthttpclient.execute(httpget);
it results in exception.
java.net.sockettimeoutexception: read timed out @ java.net.socketinputstream.socketread0(native method) @ java.net.socketinputstream.read(unknown source) ... @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:805) @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:784)
you can choose selectively process exception catching , handling ioexception
later.
try { // code } catch (sockettimeoutexception e) { // handle timeouts e.printstacktrace(); } catch (ioexception e) { // handle other io exceptions e.printstacktrace(); }
Comments
Post a Comment