gpt4 book ai didi

java - Android - 如何使用 HttpClient 接受任何证书并同时传递证书

转载 作者:太空狗 更新时间:2023-10-29 13:39:26 24 4
gpt4 key购买 nike

我有一个场景,我必须将证书传递给我的服务器,然后服务器向我发送他的证书,我必须接受该证书才能访问服务器。我为此使用了 HttpURLConnection,没有任何问题。

但是,我最近遇到了 HttpURLConnection 的问题。我使用的代码从 HTTPS 服务器检索图像。如果图像很小 (< 500kb),则不会出现任何问题。然而,对于更大的图像,我得到了这个:

javax.net.ssl.SSLProtocolException: Read error: ssl=0x3c97e8: Failure in SSL library, 通常是协议(protocol)错误

我在 Internet 上看到它,很多人说使用 HttpClient 而不是 HttpURLConnection 是可行的方法(一个例子是这个网站 http://soan.tistory.com/62,认为它是用韩语写的,我看不懂但我认为它就是这么说的)。

这是我的旧代码,使用 URLConnection:

    public static URLConnection CreateFromP12(String uri, String keyFilePath,
String keyPass, TrustManager[] trustPolicy, HostnameVerifier hv) {
try {

SSLContext sslContext = SSLContext.getInstance("TLS");
KeyStore keyStore = KeyStore.getInstance("PKCS12");
KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");

keyStore.load(new FileInputStream(keyFilePath),
keyPass.toCharArray());

kmf.init(keyStore, keyPass.toCharArray());
sslContext.init(kmf.getKeyManagers(), trustPolicy, null);

HttpsURLConnection.setDefaultSSLSocketFactory(sslContext
.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(hv);
} catch (Exception ex) {
return null;
}

URL url;
URLConnection conn;
try {
url = new URL(uri);
conn = url.openConnection();
} catch (MalformedURLException e) {
return null;
} catch (IOException e) {
return null;
}

return conn;
}

这是新的,使用 HttpClient:

public class HttpC2Connection {

public static HttpEntity CreateHttpEntityFromP12(String uri,
String keyFilePath, String keyPass) throws Exception {

KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new FileInputStream(keyFilePath), keyPass.toCharArray());

SSLSocketFactory sf = new MySSLSocketFactory(keyStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

SchemeRegistry registry = new SchemeRegistry();

registry.register(new Scheme("https", sf, 443));

ClientConnectionManager ccm = new ThreadSafeClientConnManager(params,
registry);

HttpClient httpclient = new DefaultHttpClient(ccm, params);
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();

return entity;
}

但是现在,使用 HttpClient,我的服务器返回一个错误,说我必须通过证书,所以我猜

SSLSocketFactory sf = new MySSLSocketFactory(keyStore); 

没有加载我的证书。

那么,我怎样才能同时做以下两件事:

1.) 将证书传递到我的服务器;2.) 接受来 self 的服务器的任何证书

使用 HttpClient 类?

PS:我用的是安卓3.0

谢谢

最佳答案

以下代码禁用对 HttpsUrlConnection 的任何新实例的 SSL 证书检查:

https://gist.github.com/aembleton/889392

/**
* Disables the SSL certificate checking for new instances of {@link HttpsURLConnection} This has been created to
* aid testing on a local box, not for use on production.
*/
private static void disableSSLCertificateChecking() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}

@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}

@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}
} };

try {
SSLContext sc = SSLContext.getInstance("TLS");

sc.init(null, trustAllCerts, new java.security.SecureRandom());

HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

关于java - Android - 如何使用 HttpClient 接受任何证书并同时传递证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7574237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com