如何使用AsyncHttpClient进行HTTPS电话? [英] how to make HTTPS calls using AsyncHttpClient?

查看:416
本文介绍了如何使用AsyncHttpClient进行HTTPS电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用 AsyncHttpClient 链接制作HTTP调用,但现在我们的服务器已迁移到HTTPS和我得到异常 javax.net.ssl​​.SSLPeerUnverifiedException:没有对方的证书。 有没有人试图使HTTPS调用使用这个库?

AsyncHttpClient初始化: -

  AsyncHttpClient客户端=新AsyncHttpClient();
            PersistentCookieStore myCookieStore =新PersistentCookieStore(
                    getActivity());
            //名单,其中,饼干>饼干= myCookieStore.getCookies();
            myCookieStore.clear();
            //饼干= myCookieStore.getCookies();
            client.setCookieStore(myCookieStore);

            client.get(loginUrl,新JsonHtt presponseHandler(){

                @覆盖
                公共无效的OnStart(){
                    super.onStart();
                    progressBar.setVisibility(View.VISIBLE);
                }

                @覆盖
                公共无效onFinish(){
                    super.onFinish();
                    progressBar.setVisibility(View.GONE);
                }

                @覆盖
                公共无效的onSuccess(INT状态code,JSONObject的用户信息){
                    super.onSuccess(状态code,用户信息);

                    字符串ERRORMSG = NULL;
                    尝试 {
                        ERRORMSG = userInfo.getString(错误);
                    }赶上(JSONException E){
                        e.printStackTrace();
                    }

                    如果(ERRORMSG!= NULL){
                        ERRORMSG = getActivity()。getResources()的getString(
                                R.string.loginFailure)
                                +\ n错误:+ ERRORMSG;
                        tvLoginFailure.setText(ERRORMSG);
                        tvLoginFailure.setVisibility(View.VISIBLE);

                    } 其他 {
                        Subscriber.setEmail(电子邮件);
                        Subscriber.setPassword(密码);
                        LoginUtility.saveUserInfo(getActivity(),用户信息);

                        如果(Subscriber.getStatus()。contentEquals(激活)){
                            意向意图;
                            如果(MyApplication.ottMode){
                                意图=新的意图(getActivity()
                                        OTTMainScreen.class);

                            } 其他 {
                                意图=新的意图(getActivity()
                                        MainActivity.class);
                                intent.putExtra(SIGNEDIN,真正的);
                            }
                            如果(MyApplication.ottMode){
                                Utility.playSound(getActivity());
                            }
                            startActivity(意向);
                            getActivity()完成()。

                        }否则,如果(Subscriber.getStatus()。contentEquals(
                                悬)){
                            尝试 {
                                字符串suspendedReason = USERINFO
                                        .getString(suspendreason);
                                如果(suspendedReason!= NULL
                                        &功放;&安培; suspendedReason
                                                .contentEquals(NO_SUBSCRIPTION)){

                                    新AlertDialog.Builder(getActivity())
                                            .setIcon(
                                                    android.R.drawable.ic_dialog_alert)
                                            .setTitle(帐户已暂停)
                                            .setMessage(
                                                    您的帐户没有任何活动的订阅。您需要订阅包,然后才能继续。)
                                            .setPositiveButton(
                                                    订阅,
                                                    新DialogInterface.OnClickListener(){
                                                        公共无效的onClick(
                                                                DialogInterface对话框,
                                                                其中INT){
                                                            recreatePackage();
                                                        }
                                                    })
                                            .setNegativeButton(取消,NULL)
                                            。显示();

                                } 其他 {
                                    // 去做
                                }
                            }赶上(JSONException E){
                                e.printStackTrace();
                            }

                        }否则,如果(Subscriber.getStatus()。contentEquals(INIT)){
                            // 去做
                        }
                    }
                }

                @覆盖
                公共无效onFailure(INT状态code,
                        org.apache.http.Header []头,字符串responseBody,
                        Throwable的E){
                    super.onFailure(状态code,头,responseBody,E);
                    弦乐味精= getActivity()。getResources()的getString(
                            R.string.loginFailure)
                            +\ n错误:+ responseBody;
                    tvLoginFailure.setText(MSG);
                    tvLoginFailure.setVisibility(View.VISIBLE);
                }
            });
 

解决方案

您需要导入公共服务器证书到您的默认密钥库,或者如果你不感兴趣的客户端的认证可以初始化 AsyncHttpClient

  AsyncHttpClient asycnHttpClient =新AsyncHttpClient(真,80,443);
 

但这一招并不安全,因为使用自定义的<一个href="https://github.com/loopj/android-async-http/blob/master/library/src/main/java/com/loopj/android/http/MySSLSocketFactory.java">SSLSocketFactory实施卫生组织省略了SSL证书验证,看看该<一href="https://github.com/loopj/android-async-http/blob/master/library/src/main/java/com/loopj/android/http/AsyncHttpClient.java">AsyncHttpClient来源$ C ​​$ C。

有关的SSLSocketFactory在<一个更多信息href="https://developer.android.com/reference/org/apache/http/conn/ssl/SSLSocketFactory.html">https://developer.android.com/reference/org/apache/http/conn/ssl/SSLSocketFactory.html

i was using AsyncHttpClient link for making http calls but now our server has migrated to HTTPS and I am getting exception javax.net.ssl.SSLPeerUnverifiedException: No peer certificate . Has anyone tried making https call using this library ?

initialization of AsyncHttpClient :-

AsyncHttpClient client = new AsyncHttpClient();
            PersistentCookieStore myCookieStore = new PersistentCookieStore(
                    getActivity());
            // List<Cookie> cookies = myCookieStore.getCookies();
            myCookieStore.clear();
            // cookies = myCookieStore.getCookies();
            client.setCookieStore(myCookieStore);

            client.get(loginUrl, new JsonHttpResponseHandler() {

                @Override
                public void onStart() {
                    super.onStart();
                    progressBar.setVisibility(View.VISIBLE);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onSuccess(int statusCode, JSONObject userInfo) {
                    super.onSuccess(statusCode, userInfo);

                    String errorMsg = null;
                    try {
                        errorMsg = userInfo.getString("error");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    if (errorMsg != null) {
                        errorMsg = getActivity().getResources().getString(
                                R.string.loginFailure)
                                + "\nError: " + errorMsg;
                        tvLoginFailure.setText(errorMsg);
                        tvLoginFailure.setVisibility(View.VISIBLE);

                    } else {
                        Subscriber.setEmail(email);
                        Subscriber.setPassword(password);
                        LoginUtility.saveUserInfo(getActivity(), userInfo);

                        if (Subscriber.getStatus().contentEquals("ACTIVE")) {
                            Intent intent;
                            if (MyApplication.ottMode) {
                                intent = new Intent(getActivity(),
                                        OTTMainScreen.class);

                            } else {
                                intent = new Intent(getActivity(),
                                        MainActivity.class);
                                intent.putExtra("SIGNEDIN", true);
                            }
                            if (MyApplication.ottMode) {
                                Utility.playSound(getActivity());
                            }
                            startActivity(intent);
                            getActivity().finish();

                        } else if (Subscriber.getStatus().contentEquals(
                                "SUSPENDED")) {
                            try {
                                String suspendedReason = userInfo
                                        .getString("suspendreason");
                                if (suspendedReason != null
                                        && suspendedReason
                                                .contentEquals("NO_SUBSCRIPTION")) {

                                    new AlertDialog.Builder(getActivity())
                                            .setIcon(
                                                    android.R.drawable.ic_dialog_alert)
                                            .setTitle("Account Suspended")
                                            .setMessage(
                                                    "Your account doesn't have any active subscription. You need to subscribe to a Package before you can proceed.")
                                            .setPositiveButton(
                                                    "Subscribe",
                                                    new DialogInterface.OnClickListener() {
                                                        public void onClick(
                                                                DialogInterface dialog,
                                                                int which) {
                                                            recreatePackage();
                                                        }
                                                    })
                                            .setNegativeButton("Cancel", null)
                                            .show();

                                } else {
                                    // TODO
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        } else if (Subscriber.getStatus().contentEquals("INIT")) {
                            // TODO
                        }
                    }
                }

                @Override
                public void onFailure(int statusCode,
                        org.apache.http.Header[] headers, String responseBody,
                        Throwable e) {
                    super.onFailure(statusCode, headers, responseBody, e);
                    String msg = getActivity().getResources().getString(
                            R.string.loginFailure)
                            + "\nError: " + responseBody;
                    tvLoginFailure.setText(msg);
                    tvLoginFailure.setVisibility(View.VISIBLE);
                }
            });

解决方案

You need import the public server certificate into your default keystore, or if you are not interested in the authentication of your client you can initialize the AsyncHttpClient with

AsyncHttpClient asycnHttpClient = new AsyncHttpClient(true, 80, 443);

but this trick is not secure because use a custom SSLSocketFactory implementation whos omit the SSL certificate validation, take a look at the AsyncHttpClient source code.

More information about SSLSocketFactory at https://developer.android.com/reference/org/apache/http/conn/ssl/SSLSocketFactory.html

这篇关于如何使用AsyncHttpClient进行HTTPS电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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