gpt4 book ai didi

java - 使用 SSL 协议(protocol)的 AsyncHttpClient

转载 作者:行者123 更新时间:2023-12-01 13:20:36 25 4
gpt4 key购买 nike

我正在尝试创建我的第一个 Android 应用,但遇到了问题。

我有使用 https 协议(protocol)的 Web 服务,用于从我使用的服务器获取数据 AsyncHttpClient图书馆,

它一直使用http,但是一旦我将其更改为https,它就会给我一个错误:

No peer certificate

我在网上做了一些研究,我找到了一些如何修复它的建议,但我有能力让它发挥作用。

这只是我尝试过的一些链接: link 1 link 2

我的调用网络服务的代码:

    AsyncHttpClient client = new AsyncHttpClient();

client.get(QUERY_URL,
new JsonHttpResponseHandler() {

@Override
public void onSuccess(JSONObject jsonObject) {

Log.d("test", jsonObject.toString());
}

@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {

Log.e("test", "Error: " + statusCode + " " + throwable.getMessage());
}
});

也许您推荐一些其他库来调用 https 异步?

最佳答案

这是我用于 SSL 网络的代码:

private class MobHawkCheck extends AsyncTask<String, Void, JSONObject> {

protected JSONObject doInBackground(String... params) {
JSONObject json = null;
try {
// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// From https://www.washington.edu/itconnect/security/ca/load-der.crt
InputStream cert = mActivity.getResources().openRawResource(R.raw.mycertificate);
InputStream caInput = new BufferedInputStream(cert);
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
caInput.close();
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

// Create an SSLContext that uses our TrustManager
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);

// Tell the URLConnection to use a SocketFactory from our SSLContext
URL url = new URL("https://mysecureurl.com");
HttpsURLConnection urlConnection =
(HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());

BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
json = new JSONObject(total.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
}
return json;
}

protected void onPostExecute(JSONObject result) {
//TODO parse the result JSONObject
}
}

请注意,我使用自己的 .crt 文件。您应该使用服务器证书的数据生成您的证书。

关于java - 使用 SSL 协议(protocol)的 AsyncHttpClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22045697/

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