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

Why did the author use EntityUtils.consume(httpEntity);?

It really boils down to being a “good citizen” (and really knowing the contracts of HTTPClient interfaces). What EntityUtils.consume will do is release all resources held by the httpEntity, which essentially implies releasing any underlying Stream and giving the Connection object back to its pool (in the case your connection manager is a multithreaded one) … Read more

How to set TLS version on apache HttpClient

The solution is: SSLContext sslContext = SSLContexts.custom() .useTLS() .build(); SSLConnectionSocketFactory f = new SSLConnectionSocketFactory( sslContext, new String[]{“TLSv1”, “TLSv1.1”}, null, BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); httpClient = HttpClients.custom() .setSSLSocketFactory(f) .build(); This requires org.apache.httpcomponents.httpclient 4.3.x though.

Invalid use of BasicClientConnManager: connection still allocated

Is there anything wrong I am doing here? Yes. As stated in the docs: BasicClientConnectionManager is a simple connection manager that maintains only one connection at a time. Even though this class is thread-safe it ought to be used by one execution thread only. BasicClientConnectionManager will make an effort to reuse the connection for subsequent … Read more

Mocking Apache HTTPClient using Mockito

Here is what I did to test my code using Mockito and Apache HttpBuilder: Class under test: import java.io.BufferedReader; import java.io.IOException; import javax.ws.rs.core.Response.Status; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class StatusApiClient { private static final Logger LOG = LoggerFactory.getLogger(StatusApiClient.class); private String targetUrl = “”; private HttpClient client = … Read more

Apache HttpClient 4.0.3 – how do I set cookie with sessionID for POST request?

I am so glad to solve this problem: HttpPost httppost = new HttpPost(postData); CookieStore cookieStore = new BasicCookieStore(); BasicClientCookie cookie = new BasicClientCookie(“JSESSIONID”, getSessionId()); //cookie.setDomain(“your domain”); cookie.setPath(“https://stackoverflow.com/”); cookieStore.addCookie(cookie); client.setCookieStore(cookieStore); response = client.execute(httppost); So Easy!

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