Setting connection timeout and other timeouts with HttpClient
Following code snippet demonstrates how to configure “connection-timeout”, “socket-timeout” with HttpClient
package RestAssured;
import java.io.IOException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
public class ConnectionTimeout {
public static void main(String[] args) {
int timeout = 5;
RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000)
.setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build();
CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
HttpGet method = new HttpGet(
“http://thecatapi.com/api/images/vote?api_key=xxxxxxxx&sub_id=12345&image_id=bC24&score=10”);
try {
CloseableHttpResponse response = client.execute(method);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
0 comments