当我尝试从我的Android应用程序访问HTTPS URL(.Net Webservice)时,我收到此异常:
javax.net.ssl.SSLException:不受信任的服务器证书
这是我的代码:
HttpParams myParams = new BasicHttpParams();
HttpProtocolParams.setVersion(myParams, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(myParams, "utf-8");
myParams.setBooleanParameter("http.protocol.expect-continue", false);
HttpConnectionParams.setConnectionTimeout(myParams, 100000);
HttpConnectionParams.setSoTimeout(myParams, 100000);
//
KeyStore trusted;
try {
trusted = KeyStore.getInstance("pkcs12");
trusted.load(null, "".toCharArray());
SSLSocketFactory sslf = new SSLSocketFactory(trusted);
sslf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", sslf, 443));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(myParams, schemeRegistry);
httpClient = new DefaultHttpClient(manager, myParams);
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch …Run Code Online (Sandbox Code Playgroud)