Apache HttpClient GET with body

From what I know, you can’t do this with the default HttpGet class that comes with the Apache library. However, you can subclass the HttpEntityEnclosingRequestBase entity and set the method to GET. I haven’t tested this, but I think the following example might be what you’re looking for: import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase … Read more

Apache HttpClient 4.1 – Proxy Authentication

For anyone looking for the answer for 4.3…its fairly new and their example didn’t use the new HttpClientBuilder…so this is how I implemented this in that version: NTCredentials ntCreds = new NTCredentials(ntUsername, ntPassword,localMachineName, domainName ); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxyHost,proxyPort), ntCreds ); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.useSystemProperties(); clientBuilder.setProxy(new HttpHost(pxInfo.getProxyURL(), pxInfo.getProxyPort())); clientBuilder.setDefaultCredentialsProvider(credsProvider); clientBuilder.setProxyAuthenticationStrategy(new … Read more

What does setDefaultMaxPerRoute and setMaxTotal mean in HttpClient?

Some parameters are explained at http://hc.apache.org/httpclient-3.x/preference-api.html Others must be gleaned from the source. setMaxTotal The maximum number of connections allowed across all routes. setDefaultMaxPerRoute The maximum number of connections allowed for a route that has not been specified otherwise by a call to setMaxPerRoute. Use setMaxPerRoute when you know the route ahead of time and … Read more

How to send parallel GET requests and wait for result responses?

Just in general, you need to encapsulate your units of work in a Runnable or java.util.concurrent.Callable and execute them via java.util.concurrent.Executor (or org.springframework.core.task.TaskExecutor). This allows each unit of work to be executed separately, typically in an asynchronous fashion (depending on the implementation of the Executor). So for your specific problem, you could do something like … Read more

Turning on Logging in HttpClient

I faced same problem today. And I found out a solution for this problem. It’s pretty simple. Just add jcl-over-slf4j dependency to forward JCL logging (Apache Commons Logging) to slf4j. <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</version> </dependency> After that you can config log file as Log4j Examples. If you use logback you can put the following to … Read more

Connection and connection request timeout

HttpClient has a way to set connection and socket timeout (setConnectionTimeout() and setTimeout()) according to the HttpClient javadocs. Connection timeout is the timeout until a connection with the server is established. Socket timeout is the timeout to receive data (socket timeout). Example: Let’s say you point your browser to access a web page. If the … Read more

Retry java RestTemplate HTTP request if host offline

I had same situation and done some googling found the solution. Giving answer in hope it help someone else. You can set max try and time interval for each try. @Bean public RetryTemplate retryTemplate() { int maxAttempt = Integer.parseInt(env.getProperty(“maxAttempt”)); int retryTimeInterval = Integer.parseInt(env.getProperty(“retryTimeInterval”)); SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); retryPolicy.setMaxAttempts(maxAttempt); FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy(); backOffPolicy.setBackOffPeriod(retryTimeInterval); … Read more