【问题标题】:Intermittent PeerUnverifiedException: No peer certificate with Android client间歇性 PeerUnverifiedException:Android 客户端没有对等证书
【发布时间】:2015-09-06 04:16:03
【问题描述】:

我正在尝试使用 HTTPS 实现 Android ↔ Apache 通信,但出现以下错误。我间歇性地遇到这个问题,大约有 30% 的时间。

javax.net.ssl.sslPeerUnverifiedException: No peer certificate

我在网上搜索过,但任何答案都对我有所帮助...

这是我的 Android 代码:

http_post = new HttpPost(Utils.IP_ADDRESS);
http_post_data = new ArrayList<NameValuePair>();
http_post_data.add(new BasicNameValuePair("regId", regid));
http_post_data.add(new BasicNameValuePair("email", globals.userInfo.mail));
http_post_data.add(new BasicNameValuePair("pass", globals.userInfo.pass));
http_post.setEntity(new UrlEncodedFormEntity(http_post_data));

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, Utils.TIMEOUT_CONNECTION);
HttpConnectionParams.setSoTimeout(httpParameters, Utils.TIMEOUT_SOCKET);

http_client = new DefaultHttpClient(httpParameters);
response = http_client.execute(http_post);
String responseBody = EntityUtils.toString(response.getEntity(), GlobalsSingleton.applicationCharset);

我有 GoDaddy 证书。那么我需要在我的服务器或 Android 代码中进行哪些更改才能修复此问题?

【问题讨论】:

  • @Fernando - 您需要能够复制问题才能解决问题。对于故障排除,请使用openssl s_client -connect host:port -tls1 -servername host。此外,您需要发布详细信息,例如 URL 或服务器名称,以便我们尝试复制它。拒绝我们提供详细信息没有任何用处,因为该服务器位于网络上,供坏人攻击。
  • @Fernando - 网站托管在哪里,是否负载平衡?另请参阅 SSLSocketFactoryEx 以确保您从 Android 获得配置良好的 SSL/TLS 套接字。您可以在Which Cipher Suites to enable for SSL Socket? 找到它

标签: android apache ssl https ssl-certificate


【解决方案1】:

替换套接字中的 x509TrustManager。像这样包装你的客户:

public static org.apache.http.client.HttpClient wrapClient(org.apache.http.client.HttpClient base) {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
                public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {}
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("https", 443, ssf));
            ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry);
            return new DefaultHttpClient(mgr, base.getParams());
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

【讨论】:

    【解决方案2】:

    我能够通过重新启动模拟器或创建一个新的模拟器来为自己解决这个问题,并在那个新的模拟器中运行项目。

    【讨论】:

      猜你喜欢
      • 2011-04-22
      • 2011-07-03
      • 2019-10-25
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多