android asynchttpclient javax.net.ssl.SSLHandshakeException: 握手失败

android asynchttpclient javax.net.ssl.SSLHandshakeException: Handshake failed

嗨,我在 android 中遇到了一个非常烦人的 asynchttpclient 问题。当我尝试向服务器发送请求时出现此错误:

onFailure:: javax.net.ssl.SSLHandshakeException: Handshake failed

我尝试了 postman 和浏览器,它没有任何问题,我得到了回复,但在应用程序中它不起作用。

服务器有有效的 ssl 证书,我什至试图禁用 ssl 验证检查,但我仍然得到这个愚蠢的错误。

myapikey 是我不允许与您分享的代码。

MainActivity.java:

if (getSharedPreferences("guest_token", MODE_PRIVATE).getString("gtkn",null) == null){
        AsyncHttpClient client = new AsyncHttpClient(true,80,443);
        client.addHeader("Authorization", "myapikey");

        client.setSSLSocketFactory(
                new SSLSocketFactory(getSslContext(),
                        SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));

        client.get("https://dev.sarshomar.com/api/v1/token/guest", new TextHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject m_obj = new JSONObject(responseString);
                    String guest_token = m_obj.getJSONObject("msg").getJSONObject("callback").getString("token");

                    SharedPreferences pref = getSharedPreferences("guest_token", MODE_PRIVATE);
                    SharedPreferences.Editor editor = pref.edit();
                    editor.putString("gtkn", guest_token);
                    editor.apply();

                    Log.d("onSuccess_token: ", guest_token);
                    Toast.makeText(getApplicationContext(),guest_token,Toast.LENGTH_LONG).show();

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();

                }

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String errorresponse, Throwable throwable) {
                throwable.printStackTrace();
                Log.d("onFailure: ",throwable.toString()+errorresponse+statusCode+throwable.getLocalizedMessage());
            }

            @Override
            public void onStart() {
                // called before request is started
            }


            @Override
            public void onRetry(int retryNo) {
                // called when request is retried
            }
        });
    }
}

public SSLContext getSslContext() {

    TrustManager[] byPassTrustManagers = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }
    } };

    SSLContext sslContext=null;

    try {
        sslContext = SSLContext.getInstance("TLS");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    try {
        sslContext.init(null, byPassTrustManagers, new SecureRandom());
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    return sslContext;
}

无论我尝试

我都会出错
AsyncHttpClient client = new AsyncHttpClient(true,80,443)

AsyncHttpClient client = new AsyncHttpClient();

根据 SSLLabs the site works only with clients supporting SNI. But according to this bug report AsyncHttpClient 不支持 SNI。这样你就握手失败了。

好的,我开始使用 Volley 而不是 loopj 的 AsyncHttpclient,它就像一个魅力,所以如果其他人遇到与我相同的问题,请改用 volley,它非常容易学习和使用。