In general, all you have to do is create an instance of HttpParams and use it in order to define the connection timeout.
Let's see how the code looks:
package com.bashan.blog.http;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.params.BasicHttpParams;import org.apache.http.params.HttpConnectionParams;import org.apache.http.params.HttpParams;import org.apache.http.util.EntityUtils;/**
* @author Bashan*/public class HttpRequest {public static void main(String[] args) throws Exception {// Set connection timeoutHttpParams params = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(params, 5000);HttpConnectionParams.setSoTimeout(params, 5000);HttpClient httpClient = new DefaultHttpClient(params);HttpResponse response = httpClient.execute(new HttpGet("http://google.com"));System.out.print(EntityUtils.toString(response.getEntity()));}}
You can see in the 3 lines marked in yellow, how the HttpParams class is used in order to define the connection timeout. Later an HttpClient instance is created with params that is passed to it.
You can download this class here.
Thanks a lot !
ReplyDeleteThanks!
ReplyDeleteThanks!
ReplyDeletedoes not work....easy break....run in windows 7..............timeout is totally ignored........
ReplyDeleteAre you sure? This is kinda' strange. I don't think a timeout on Java is related with Windows 7...
ReplyDelete