2 Minutes To Code (β Version)

Apache Commons Http Client - 2 minute tutorial

Setting timeout : How to set conenction timeout and read time out

This example shows how to configure the HttpClient to use a connection timeout and a read timeout.

The operating code here is httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(3000) to set the connection time-out as 3000 milliseconds.

Use httpClient.getHttpConnectionManager().getParams().setSoTimeout(2000) to set the read timeout as 2000 milliseconds.
package com.techfundaes.httpBag;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

public class SimpleGetWithTimeout 
{
	public static void main(String[] args)
	{
		HttpClient httpClient = new HttpClient();
		GetMethod getMethod = new GetMethod("http://www.techfundaes.com/test.do");
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
		httpClient.getHttpConnectionManager().getParams().setSoTimeout(2000);
		try
		{
			httpClient.executeMethod(getMethod);
			System.out.println("responseString:"+getMethod.getResponseBodyAsString());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			getMethod.releaseConnection();
		}
	}
}