Apache HttpComponents HttpClient timeout

In version 4.3 of Apache Http Client the configuration was refactored (again). The new way looks like this: RequestConfig.Builder requestBuilder = RequestConfig.custom(); requestBuilder.setConnectTimeout(timeout); requestBuilder.setConnectionRequestTimeout(timeout); HttpClientBuilder builder = HttpClientBuilder.create(); builder.setDefaultRequestConfig(requestBuilder.build()); HttpClient client = builder.build();

How do Jersey-client and Apache HTTP Client compare?

These two things probably should not be compared directly. Jersey is a REST-client, featuring full JAX-RS implementation, neat fluent API and a powerfull filter stack. Apache Http Client is a HTTP-client, perfect in managing low-level details like timeouts, complex proxy routes and connection polling. They act on a different levels of your protocol stack. When … Read more

Exception : javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

Expired certificate was the cause of our “javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”. keytool -list -v -keystore filetruststore.ts Enter keystore password: Keystore type: JKS Keystore provider: SUN Your keystore contains 1 entry Alias name: somealias Creation date: Jul 26, 2012 Entry type: PrivateKeyEntry Certificate chain length: 1 Certificate[1]: Owner: CN=Unknown, OU=SomeOU, O=”Some Company, Inc.”, L=SomeCity, ST=GA, C=US … Read more

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);

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

Write in body request with HttpClient

If your xml is written by java.lang.String you can just using HttpClient in this way public void post() throws Exception{ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(“http://www.baidu.com”); String xml = “<xml>xxxx</xml>”; HttpEntity entity = new ByteArrayEntity(xml.getBytes(“UTF-8”)); post.setEntity(entity); HttpResponse response = client.execute(post); String result = EntityUtils.toString(response.getEntity()); } pay attention to the Exceptions. BTW, … Read more

How to prevent hangs on SocketInputStream.socketRead0 in Java?

Though this question mentions Windows, I have the same problem on Linux. It appears there is a flaw in the way the JVM implements blocking socket timeouts: https://bugs.openjdk.java.net/browse/JDK-8049846 https://bugs.openjdk.java.net/browse/JDK-8075484 To summarize, timeout for blocking sockets is implemented by calling poll on Linux (and select on Windows) to determine that data is available before calling recv. … Read more

Apache HttpClient making multipart form post

Use MultipartEntityBuilder from the HttpMime library to perform the request you want. In my project I do that this way: HttpEntity entity = MultipartEntityBuilder .create() .addTextBody(“number”, “5555555555”) .addTextBody(“clip”, “rickroll”) .addBinaryBody(“upload_file”, new File(filePath), ContentType.APPLICATION_OCTET_STREAM, “filename”) .addTextBody(“tos”, “agree”) .build(); HttpPost httpPost = new HttpPost(“http://some-web-site”); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); HttpEntity result = response.getEntity(); Hope this will help. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)