android应用程序出错:javax.net.ssl.SSLPeerUnverifiedException:没有对等证书

时间:2015-05-14 08:16:01

标签: android security ssl https

美好的一天,可能会征求意见。 我的代码工作正常,但有一天没有明显的原因,并且没有更改服务器上的代码停止工作,显示此错误 我检查了链接所引用的决定,但没有一个给出了预期的结果。 也许SSL协议中有一些东西?

Safely fixing: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

javax.net.ssl.sslpeerunverifiedexception no peer certificate

Android ssl: javax.net.ssl.SSLPeerUnverifiedException: No peer certificate

Trusting all certificates using HttpClient over HTTPS

class RequestTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {

        try {

            HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

            DefaultHttpClient client = new DefaultHttpClient();

            SchemeRegistry registry = new SchemeRegistry();
            SSLSocketFactory socketFactory = SSLSocketFactory
                    .getSocketFactory();
            socketFactory
                    .setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
            registry.register(new Scheme("https", socketFactory, 443));
            SingleClientConnManager mgr = new SingleClientConnManager(
                    client.getParams(), registry);
            DefaultHttpClient httpClient = new DefaultHttpClient(mgr,
                    client.getParams());

            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            ResponseHandler<String> res = new BasicResponseHandler();

            HttpPost postMethod = new HttpPost("https://api.equalibra.net/banners.json");

            nameValuePairs.add(new BasicNameValuePair("lang", "rus"));
            postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs,
                    HTTP.UTF_8));

            String response = "";
            try {
                response = httpClient.execute(postMethod, res);
            } catch (Exception e) {
                Log.d("my", e.toString());
            }

            JSONURL(response);

        } catch (Exception e) {
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
}

2 个答案:

答案 0 :(得分:2)

服务器需要使用SNI (server name indication)。不使用SNI的连接只会失败,而不是通常的情况下,他们获得默认证书。由于这是服务器端的设置,因此最近可能已更改,因此会破坏您的代码。

虽然SNI在较新版本的Apache HTTP客户端库中可用,但在您可能使用的Android SDK附带的版本中不可用。

答案 1 :(得分:1)

确实,问题在于我的Android SDK不支持SNI。  It helped me: