javax.net.ssl.SSLException: hostname in certificate didn't match证书不匹配的主机名(使用的是AsyncHttpClient)

最近做项目对接https的接口,突然出现这种问题,之前还没有出现过,具体原因好像是后台设置了多个站点的原因,因为单个站点是没有问题的。具体原因没有深究。下面是我的解决方案,搜了好多,在一个博客底下的评论找到了方向,感谢。方案如下:

import com.loopj.android.http.AsyncHttpClient;

import com.loopj.android.http.AsyncHttpResponseHandler;

import com.loopj.android.http.MySSLSocketFactory;

import com.loopj.android.http.RequestParams;

import java.security.KeyStore;

import cz.msebera.android.httpclient.HttpVersion;

import cz.msebera.android.httpclient.conn.scheme.PlainSocketFactory;

import cz.msebera.android.httpclient.conn.scheme.Scheme;

import cz.msebera.android.httpclient.conn.scheme.SchemeRegistry;

import cz.msebera.android.httpclient.conn.ssl.SSLSocketFactory;

import cz.msebera.android.httpclient.params.BasicHttpParams;

import cz.msebera.android.httpclient.params.HttpConnectionParams;

import cz.msebera.android.httpclient.params.HttpParams;

import cz.msebera.android.httpclient.params.HttpProtocolParams;

import cz.msebera.android.httpclient.protocol.HTTP;


下面的是重点:

private static AsyncHttpClient client = new AsyncHttpClient(getSchemeRegistry());

public static SchemeRegistry getSchemeRegistry() {

    try {

        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());

        trustStore.load(null, null);

        SSLSocketFactory sf = new MySSLSocketFactory(trustStore);

       sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();

        HttpConnectionParams.setConnectionTimeout(params,10000);

        HttpConnectionParams.setSoTimeout(params,10000);

        HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);

        HttpProtocolParams.setContentCharset(params,HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();

        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(),80));

        registry.register(new Scheme("https", sf, 443));

        return registry;

    } catch (Exceptione) {

        return null;

    }

}



  publicstatic void postNoHead(Stringurl, RequestParams params, Context context,

                                 AsyncHttpResponseHandler responseHandler) {

        if (NetworkUtil.isNetworkValidate(context)){

            try {

                client.setURLEncodingEnabled(false);

                client.post(context,url, params, responseHandler);

                Log.e(TAG, "post--url--"+ url);

            } catch (Exceptione) {

                e.printStackTrace();

            }

        } else {

            ToastUtils.show(context,R.string.network_error);

        }

    }

public static void getNoHead(String url, Context context,

                            AsyncHttpResponseHandler responseHandler) {

    if (NetworkUtil.isNetworkValidate(context)){

        try {

            client.setURLEncodingEnabled(false);

            client.get(url,null, responseHandler);

        } catch (Exceptione) {

            e.printStackTrace();

        }

        Log.e(TAG, "get--url--"+ url);

    } else {

        ToastUtils.show(context,R.string.network_error);

    }

}

 

 

阅读更多
文章标签: Android
个人分类: Android
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭