【发布时间】:2015-07-09 05:33:02
【问题描述】:
我希望你们能帮助我解决这个问题,我做了所有的研究并尝试了我看到的任何东西,但它无法解决我的问题。我想要做的是信任我的应用程序中的所有 SSL 证书。我看到的所有解决方案都是使用 URLHttpConnection,但我需要一个适用于 AndroidHttpClient 的工作解决方案。请参阅下面的代码:
AndroidHttpClient httpClient = null;
HttpResponse httpResponse;
Bundle responseBundle;
try{
httpClient = AndroidHttpClient.newInstance("android");
httpClient = addCustomCertificate(httpClient);
httpResponse = httpClient.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String response = convertStreamToString(instream);
responseBundle = new Bundle();
responseBundle.putString("result", response);
responseBundle.putInt("responseCode", responseCode);
receiver.send(method, responseBundle);
instream.close();
httpClient.close();
}
}
//====
private AndroidHttpClient addCustomCertificate(AndroidHttpClient client)
{
SSLSocketFactory sf = SSLSocketFactory.getSocketFactory();
try
{
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
sf = new SSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}
catch (Exception t)
{
t.printStackTrace();
}
client.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sf, 443));
return client;
}
但我总是在日志中捕获的图像中显示错误。我不知道我还能做什么其他解决方案。
【问题讨论】:
-
@RemeesMSyde 是的,实际上我创建的代码来自那些。
标签: android ssl-certificate androidhttpclient