当我尝试调用我的网络服务时,我遇到了 4.1 及更高版本的 android 设备的问题。我已将 .cer 文件转换为 bks 并使用以下代码:
KeyStore trusted = KeyStore.getInstance("BKS");
InputStream in = c.getResources().openRawResource(R.raw.test);
trusted.load(in, "password".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("X509");
trustManagerFactory.init(trusted);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
kmf.init(trusted, "password".toCharArray());
SSLContext context = SSLContext.getInstance("TLS");
context.init(kmf.getKeyManagers(), tm, null);
URL request = new URL(URL);
urlConnection = (HttpsURLConnection) request.openConnection();
urlConnection.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
urlConnection.setSSLSocketFactory(context.getSocketFactory());
urlConnection.setDoInput(true);
urlConnection.setReadTimeout(200000);
urlConnection.connect();
InputStream ina = urlConnection.getInputStream();
BasicHttpEntity res = new BasicHttpEntity();
res.setContent(ina);
HttpResponse resp = new BasicHttpResponse(HttpVersion.HTTP_1_1,
urlConnection.getResponseCode(), "");
resp.setEntity(res);
HttpEntity responseEntity = resp.getEntity();
InputStream data = responseEntity.getContent();
此代码适用于 4.0 及以下设备,但对于 4.1 作为响应,我收到“ 403 禁止错误”。
不幸的是,我无法找到哪里出错了,没有任何帮助。