Using Apache httpclient for https

I put together this test app to reproduce the issue using the HTTP testing framework from the Apache HttpClient package: ClassLoader cl = HCTest.class.getClassLoader(); URL url = cl.getResource(“test.keystore”); KeyStore keystore = KeyStore.getInstance(“jks”); char[] pwd = “nopassword”.toCharArray(); keystore.load(url.openStream(), pwd); TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm()); tmf.init(keystore); TrustManager[] tm = tmf.getTrustManagers(); KeyManagerFactory kmfactory = KeyManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm()); kmfactory.init(keystore, pwd); … Read more

Apache HTTP client or URLConnection [duplicate]

Google has silently deprecated Apache HTTP client usage since Gingerbread: http://android-developers.blogspot.com/2011/09/androids-http-clients.html. And while they didn’t mark it with deprecated annotation, they suggest you to use HttpURLConnection for new applications as: it is where we [Google] will be spending our energy going forward. Personally I don’t like that decision and would rather stick to HttpClient 4.1+, … Read more

Apache HttpClient Digest authentication

This code snippet worked for me. You have to provide the realm which you can get by looking at the 401 response header you get from the host. val credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); val authCache = new BasicAuthCache(); val digestScheme = new DigestScheme(); digestScheme.overrideParamter(“realm”, “**Name of the Realm**”); // Nonce value … Read more

Apache HttpClient on Android producing CertPathValidatorException (IssuerName != SubjectName)

I expect you’ve got your own solution by now, but if not: By combining insights from Antoine Hauck’s blog http://blog.synyx.de/2010/06/android-and-self-signed-ssl-certificates/ the excellent answer from bdc above easily-googled source code for “EasySSLSocketFactory” and “EasyX509TrustManager” – would provide a link if I wasn’t prevented (first time answering!) I managed to achieve a secure connection to https://eu.battle.net/login/en/login.xml with … Read more

Best Practice to Use HttpClient in Multithreaded Environment

Definitely Method A because its pooled and thread safe. If you are using httpclient 4.x, the connection manager is called ThreadSafeClientConnManager. See this link for further details (scroll down to “Pooling connection manager”). For example: HttpParams params = new BasicHttpParams(); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme(“http”, PlainSocketFactory.getSocketFactory(), 80)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, registry); … Read more

Disable HttpClient logging

Update log4j.properties to include: log4j.logger.httpclient.wire.header=WARN log4j.logger.httpclient.wire.content=WARN Note that if Log4j library is not installed, HttpClient (and therefore JWebUnit) will use logback. In this situation, create or edit logback.xml to include: <configuration> <logger name=”org.apache” level=”WARN” /> <logger name=”httpclient” level=”WARN” /> </configuration> Setting the log level to WARN with Log4j using the package name org.apache.commons.httpclient in log4j.properties … Read more

Deprecated Java HttpClient – How hard can it be?

Relevant imports: import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import java.io.IOException; Usage: HttpClient httpClient = HttpClientBuilder.create().build(); EDIT (after Jules’ suggestion): As the build() method returns a CloseableHttpClient which is-a AutoClosable, you can place the declaration in a try-with-resources statement (Java 7+): try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) { // use httpClient (no need to close it explicitly) } catch … Read more

DefaultHttpClient to AndroidHttpClient

StrictMode.ThreadPolicy was introduced since API Level 9 and the default thread policy had been changed since API Level 11, which in short, does not allow network operation (include HttpClient and HttpUrlConnection) get executed on UI thread. if you do this, you get NetworkOnMainThreadException. This restriction can be changed, using: if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy … Read more