使用 HttpClient 在 Android 中重用 SSL 会话 [英] Reusing SSL Sessions in Android with HttpClient

查看:38
本文介绍了使用 HttpClient 在 Android 中重用 SSL 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 HttpClient 在 Android 上恢复 SSL 会话时遇到很多困难.

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

我每 90 秒轮询一次服务器(仅适用于具有一个功能的工业设备),因此我需要恢复会话,否则数据使用量会从每小时几 kB 飙升至 150-200 kB,这是不可持续的.服务器在 Restlet 中嵌入了 Jetty,据我所知,当我使用 OpenSSL 测试它时,它支持恢复 SSL 会话.

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.

我正在重用我的 HttpClient 对象,所以不是这样.Android 有一个特定的 SSLCertificateSocketFactory,我也尝试过,但它似乎也不起作用.

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.

这里有什么我完全遗漏的吗?我原以为 HttpClient 会自动执行此操作,但我不确定自己做错了什么,而且互联网上似乎没有人遇到过类似的问题.

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.

我已经通过以下方式设置了 httpClient:

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

(我故意使用端口 444)

(I'm using port 444 on purpose)

然后我使用简单的 HttpGet 和基本授权重用 HttpClient 对象.

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

我在这里做错了什么!?任何人,请帮忙!

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

推荐答案

已修复.它现在正在使用会话并消耗极少量的数据.

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

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

删除该行修复了它,尽管 HttpClient 甚至从未使用过 http/80 端口.我不知道为什么会这样.

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

这篇关于使用 HttpClient 在 Android 中重用 SSL 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆