本文介绍了SSL不工作在Android 2.2(仅限2.3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在LogCat中得到这个被称为当httpsURLConnection.getInputStream()
我已经测试了它的Andorid 2.3和它工作得很好。
我的服务器需要客户端身份验证!也许升级Froyo不支持这种握手......我不知道......
我尝试使用HttpClient的为好。失败在任何情况下...
私人无效处理()抛出异常{
的char []通=clientpass.toCharArray();
InputStream的ksStream = getAssets()开(clientKeyStore.bks)。
密钥库的keyStore = KeyStore.getInstance(BKS);
keyStore.load(ksStream,通);
的KeyManagerFactory KMF = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(密钥库通过);
ksStream.close();
X509TrustManager [] TM =新X509TrustManager [] {新X509TrustManager(){
公共无效checkClientTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
}
公共无效checkServerTrusted(x509证书[]链,字符串的authType)抛出CertificateException {
}
公共x509证书[] getAcceptedIssuers(){
返回新的X509证书[0];
}
}};
的SSL连接上下文= SSLContext.getInstance(TLS);
context.init(kmf.getKeyManagers(),TM,NULL);
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(新的HostnameVerifier(){
公共布尔验证(字符串主机名,的SSLSession会话){
返回true;
}
});
网址URL =新的URL(https://192.168.2.101:8443/RestTomcat/resources/veiculos/KKK1234);
HttpsURLConnection httpsURLConnection =(HttpsURLConnection)url.openConnection();
的BufferedReader BR =新的BufferedReader(新的InputStreamReader(httpsURLConnection.getInputStream()));
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= br.readLine())!= NULL)
sb.append(行+\ N);
br.close();
Log.e(输出,sb.toString());
httpsURLConnection.disconnect();
}
解决方案
确认日期,时间和时区设置是2.2设备上正确的。
I'm getting this on LogCat when httpsURLConnection.getInputStream() is called
I have tested it on Andorid 2.3 and it works nicely.
My server requires client authentication! Maybe FROYO does not support this kind of handshake... I don't know...
I tried using httpclient as well. Fail in every case...
private void process() throws Exception {
char[] pass = "clientpass".toCharArray();
InputStream ksStream = getAssets().open("clientKeyStore.bks");
KeyStore keyStore = KeyStore.getInstance("BKS");
keyStore.load(ksStream, pass);
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keyStore, pass);
ksStream.close();
X509TrustManager[] tm = new X509TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} };
SSLContext context = SSLContext.getInstance("TLS");
context.init(kmf.getKeyManagers(), tm, null);
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
URL url = new URL("https://192.168.2.101:8443/RestTomcat/resources/veiculos/KKK1234");
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
sb.append(line + "\n");
br.close();
Log.e("OUTPUT", sb.toString());
httpsURLConnection.disconnect();
}
解决方案
Make sure the date, time and timezone settings are correct on the 2.2 device.
这篇关于SSL不工作在Android 2.2(仅限2.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!