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);
}
}
- httpclient对象请求:如果报这个错:javax.net.ssl.SSLException: hostname in certificate didn't match证书不匹配的主机名
- javax.net.ssl.SSLException: hostname in certificate didn't match证书不匹配的主机名
- 12. javax.net.ssl.SSLException: hostname in certificate didn't match证书不匹配的主机名
- Android 链接https出现 javax.net.ssl.SSLException: hostname in certificate didn't match:
- Android 链接https出现 javax.net.ssl.SSLException: hostname in certificate didn't match:
- HttpClient请求https,抛出javax.net.ssl.SSLException: hostname in certificate didn't match:
- Android 链接https出现 javax.net.ssl.SSLException: hostname in certificate didn't match:
- HttpsURLConnection请求https,抛出javax.net.ssl.SSLException: hostname in certificate didn't match:
- javax.net.ssl.SSLException:hostname in certificate didn't match
- Android 异常:javax.net.ssl.SSLException: hostname in certificate didn't match
- Android 链接https出现 javax.net.ssl.SSLException: hostname in certificate didn't match:
- javax.net.ssl.SSLException: hostname in certificate didn't match:
- Android javax.net.ssl.SSLException: hostname in certificate didn't match...
- javax.netssl.SSLException: hostname in certificate didn't match
- javax.net.ssl.SSLException: hostname in certificate didn't match:
- android SDK 更新出现“hostname in certificate didn't match: <dl-ssl.google.com>”解决方法
- wget 无法建立ssl连接 [ERROR: certificate common name ?..ssl.fastly.net?.doesn?. match requested host name ?.ache.ruby-lang.org?. To connect to cache.ruby-lang.org insecurely, use ?.-no-check-certificate?]
- android SDK 更新出现“hostname in certificate didn't match: <dl-ssl.google.com>”解决方法
- Unable to execute HTTP request: hostname in certificate didn't match
- Maven 报错:hostname in certificate didn’t match 的解决方法