Apache HttpClient timeout

For a newer version of httpclient (e.g. http components 4.3 – https://hc.apache.org/httpcomponents-client-4.3.x/index.html): int CONNECTION_TIMEOUT_MS = timeoutSeconds * 1000; // Timeout in millis. RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(CONNECTION_TIMEOUT_MS) .setConnectTimeout(CONNECTION_TIMEOUT_MS) .setSocketTimeout(CONNECTION_TIMEOUT_MS) .build(); HttpPost httpPost = new HttpPost(URL); httpPost.setConfig(requestConfig);

Receiving Multipart Response on client side (ClosableHttpResponse)

I have finally got a workaround for it. I will be using javax mail MimeMultipart. Below is a code snipped for the solution:- ByteArrayDataSource datasource = new ByteArrayDataSource(in, “multipart/form-data”); MimeMultipart multipart = new MimeMultipart(datasource); int count = multipart.getCount(); log.debug(“count ” + count); for (int i = 0; i < count; i++) { BodyPart bodyPart = … Read more

java.net.SocketException: Too many open files

“java.net.SocketException: Too many files open”can be seen any Java Server application e.g. Tomcat, Weblogic, WebSphere etc, with client connecting and disconnecting frequently. Please note that socket connections are treated like files and they use file descriptor, which is a limited resource. Different operating system has different limits on number of file handles they can manage. … Read more

Apache HTTP connection with Android 6.0 (Marshmallow)

This page discusses the removal of the Apache HTTP classes, and it suggests a workaround as well: To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file: android { useLibrary ‘org.apache.http.legacy’ } In my case Android Studio still complained that it couldn’t find these classes, but … Read more

How to get HttpClient returning status code and response body?

Don’t provide the handler to execute. Get the HttpResponse object, use the handler to get the body and get the status code from it directly try (CloseableHttpClient httpClient = HttpClients.createDefault()) { final HttpGet httpGet = new HttpGet(GET_URL); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { StatusLine statusLine = response.getStatusLine(); System.out.println(statusLine.getStatusCode() + ” ” + statusLine.getReasonPhrase()); String responseBody … Read more

How to prevent apache http client from following a redirect

The magic, thanks to macbirdie , is: params.setParameter(“http.protocol.handle-redirects”,false); Imports are left out, here’s a copy paste sample: HttpClient httpclient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); // HTTP parameters stores header etc. HttpParams params = new BasicHttpParams(); params.setParameter(“http.protocol.handle-redirects”,false); // Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Bind custom … Read more