httpclient: setting request timeout period, connection time, etc.

httpclient: setting request timeout period, connection time, etc.

public static void main(String[] args) throws Exception{
		
		// Create httpclient
		CloseableHttpClient httpClient = HttpClients.createDefault();
		// Create a http get
		HttpGet httpGet = new HttpGet("http://www.taotao.com/");
		// build overtime and other configuration information
		RequestConfig config = RequestConfig.custom (). SetConnectTimeout (1000) // connection time
				.setConnectionRequestTimeout (1000) // maximum time taken from the connection pool connection
				Timeout .setSocketTimeout (10 * 1000) // data transmission
				Before .setStaleConnectionCheckEnabled (true) // submit a request to test the connection is available
				.build();
		// Set Configuration request time
		httpGet.setConfig(config);
		
		// accept data returned
		CloseableHttpResponse response = null;
		
		try {
			response = httpClient.execute(httpGet);
		}finally{
			if(response!=null){
				response.close();
			}
			httpClient.close();
		}
				
		
	}

  

Guess you like

Origin www.cnblogs.com/achengmu/p/11080215.html