I am using HttpClient 4.1.2. Setting ConnectionTimeout and SocketTimeout to a value will never be effective.
the code:
Long startTime = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 30);
HttpConnectionParams.setSoTimeout(params, 60);
HttpGet httpget = new HttpGet("http://localhost:8080/Test/ScteServer");
try {
startTime = System.currentTimeMillis();
HttpResponse response = httpClient.execute(httpget);
}
catch(SocketTimeoutException se) {
Long endTime = System.currentTimeMillis();
System.out.println("SocketTimeoutException :: time elapsed :: " + (endTime-startTime));
se.printStackTrace();
}
catch(ConnectTimeoutException cte) {
Long endTime = System.currentTimeMillis();
System.out.println("ConnectTimeoutException :: time elapsed :: " + (endTime-startTime));
cte.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
Long endTime = System.currentTimeMillis();
System.out.println("IOException :: time elapsed :: " + (endTime-startTime) );
e.printStackTrace();
}
If the server is turned off, the connection timeout will never exceed 400 ms, if it should be set for ~ 30 ms.
The same applies to Socket Timeout, putting sleep in doGet () for 5000 ms, throws out a socket timeout, which will never be about 60 ms. It takes more than 500 ms.
Can anyone suggest how to configure HttpClient 4.1.2 so that it depends on the configured time from time to time?
source
share