我必须连接到一个基于REST的WebService。
(https://someurl.com/api/lookup/jobfunction/lang/en)
在IE或chrome浏览器中,当我尝试访问这个URL时,我会得到一个证书,我必须信任它并接受它才能继续,然后我必须输入用户名和密码,然后我会得到JSON响应。
同样的事情,我必须为一个android应用程序编程。
>
尝试使用自定义EasySSLSocketFactory和EasyX509TrustManager,但无效。我得到以下错误:java.security.cert.CertPathValidateXception:找不到认证路径的信任锚。
使用了BKS密钥库,请注意在我执行以下命令之前mykeystore.BKS是一个空文件
keytool -importcert -v -trustcacerts -file "test.crt" -alias IntermediateCA -keystore "mykeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "bcprov-jdk15on-148.jar" -storetype BKS -storepass abcd1234
keytool -list -keystore "mykeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "bcprov-jdk15on-148.jar" -storetype BKS -storepass abcd1234
myHttpClient.java如下所示:
public class MyHttpClient extends DefaultHttpClient {
final Context context;
public MyHttpClient(Context context) {
this.context = context;
}
@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Register for port 443 our SSLSocketFactory with our keystore
// to the ConnectionManager
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry);
}
private SSLSocketFactory newSslSocketFactory() {
try {
// Get an instance of the Bouncy Castle KeyStore format
KeyStore trusted = KeyStore.getInstance("BKS");
// Get the raw resource, which contains the keystore with
// your trusted certificates (root and any intermediate certs)
InputStream in = context.getResources().openRawResource(R.raw.mykeystore);
try {
// Initialize the keystore with the provided trusted certificates
// Also provide the password of the keystore
trusted.load(in, "abcd1234".toCharArray());
} finally {
in.close();
}
// Pass the keystore to the SSLSocketFactory. The factory is responsible
// for the verification of the server certificate.
SSLSocketFactory sf = new SSLSocketFactory(trusted);
// Hostname verification from certificate
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
return sf;
} catch (Exception e) {
throw new AssertionError(e);
}
}
当我调用webservice时,我会得到以下错误:java.lang.AssertionError:java.io.ioException:密钥存储的错误版本
请告诉我需要做什么才能连接到基于HTTPS的rest webservice,它具有用户名和密码凭据。......
我得到了别人的帮助。解决方法如下: