Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing SSL Sessions in Android with HttpClient

I'm having a lot of difficulty resuming an SSL session on Android using HttpClient.

I'm polling a server every 90 seconds (it's for industrial devices with one function only), so I need to resume the session or else data use skyrockets from a few kB an hour up to 150-200kB, which is unsustainable. The server is embedded Jetty in Restlet, and supports resumption of SSL sessions when I test it using OpenSSL as far as I can tell.

I'm reusing my HttpClient object, so it's not that. Android has a specific SSLCertificateSocketFactory which I've also tried and it also doesn't seem to work.

Is there something I'm completely missing here? I had presumed HttpClient would do this automatically, I'm not sure what I'm doing wrong, and no-one on the internet seems to be coming up against a similar problem.

I've set up the httpClient via:

    public HttpClient getNewHttpClient(Context context) {
    try {

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        HttpConnectionParams.setStaleCheckingEnabled(params, false);

        HttpConnectionParams.setConnectionTimeout(params, 4 * 1000);
        HttpConnectionParams.setSoTimeout(params, 5 * 1000);
        HttpConnectionParams.setSocketBufferSize(params, 8192);

        HttpClientParams.setRedirecting(params, false);

        SSLSessionCache sslSession = new SSLSessionCache(context);
        SchemeRegistry registry = new SchemeRegistry();

        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", SSLCertificateSocketFactory.getHttpSocketFactory(10*60*1000, sslSession), 444));
        //registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 444));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

        return new DelegateHttpClient(ccm, params);

    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

private static class DelegateHttpClient extends DefaultHttpClient {

    private DelegateHttpClient(ClientConnectionManager ccm, HttpParams params) {
      super(ccm, params);
    }

    @Override
    protected HttpContext createHttpContext() {

      HttpContext context = new BasicHttpContext();
      context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, getAuthSchemes());
      context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, getCookieSpecs());
      context.setAttribute(ClientContext.CREDS_PROVIDER, getCredentialsProvider());

      CookieStore cookieStore = new BasicCookieStore(); // Create a local instance of cookie store
      context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

      return context;
    }
  }

(I'm using port 444 on purpose)

Then just I reuse the HttpClient object using a simple HttpGet and Basic authorization.

What am I doing wrong here !? Anyone, please help !

like image 659
user705142 Avatar asked Apr 13 '11 02:04

user705142


1 Answers

It's fixed. It is now using sessions and consuming minute amounts of data.

registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

Removing that line fixes it, despite HttpClient never even using http/port 80. Why this works I have no idea.

like image 145
user705142 Avatar answered Sep 27 '22 19:09

user705142