1

Hi I have a very annoying problem with asynchttpclient in android. when I try to send a request to server I get this error:

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

I tried postman and browser and it works without any problem and i get response but in app it doesn't work.

The server has valid ssl certificate, I even tried to disable ssl validation check but still I get this stupid error.

myapikey is a code that i'm not allowed to share whit you.

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

I get error whether I try

AsyncHttpClient client = new AsyncHttpClient(true,80,443)

or

AsyncHttpClient client = new AsyncHttpClient();

2 Answers 2

1

According to SSLLabs the site works only with clients supporting SNI. But according to this bug report AsyncHttpClient does not support SNI. Thus you get a handshake failure.

Sign up to request clarification or add additional context in comments.

2 Comments

So what should I do? Use another httpclient?
@mathias.h: If you need to access the unmodified site you probably need to use another HTTP client which supports SNI. If you are able to change the configuration of the site you might change it to not require SNI.
0

OK I started using Volley instead of loopj's AsyncHttpclient and it works like a charm so if anybody else has the same problem as me use volley instead it's pretty easy to learn and use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.