gpt4 book ai didi

android - 如何在 Android 的 loopj 中支持 Comodo SSL 证书?

转载 作者:行者123 更新时间:2023-11-30 02:13:33 26 4
gpt4 key购买 nike

我正在使用 HTTPS 和来自 Comodo 的 SSL 证书运行网站. Qualys为该网站提供 A+ 评分,并且相同的 URL 在 Android 上的 Chrome 中可以正常工作。当我尝试使用 loopj 从 Android 应用程序连接到该站点时,出现了 SSLPeerUnverifiedException 异常。我必须手动提供证书信息吗?

我在默认 AsyncHttpClient 中看到了这种行为示例:

AsyncHttpClient client = new AsyncHttpClient();
client.get("https://myapp.com", new AsyncHttpResponseHandler() {

@Override
public void onStart() {
// called before request is started
}

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is "200 OK"
}

@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
}

@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});

异常(exception):

04-20 21:59:57.092: W/System.err(8824): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
04-20 21:59:57.102: W/System.err(8824): at com.android.org.conscrypt.SSLNullSession.getPeerCertificates(SSLNullSession.java:104)
04-20 21:59:57.102: W/System.err(8824): at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:93)
04-20 21:59:57.102: W/System.err(8824): at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:388)
04-20 21:59:57.102: W/System.err(8824): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:214)
04-20 21:59:57.102: W/System.err(8824): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:167)
04-20 21:59:57.102: W/System.err(8824): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125)
04-20 21:59:57.102: W/System.err(8824): at org.apache.http.impl.client.DefaultRequestDirector.executeSB(DefaultRequestDirector.java:831)
04-20 21:59:57.102: W/System.err(8824): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:697)
04-20 21:59:57.102: W/System.err(8824): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:575)
04-20 21:59:57.102: W/System.err(8824): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:491)
04-20 21:59:57.102: W/System.err(8824): at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:147)
04-20 21:59:57.102: W/System.err(8824): at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:178)
04-20 21:59:57.102: W/System.err(8824): at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:109)
04-20 21:59:57.102: W/System.err(8824): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
04-20 21:59:57.102: W/System.err(8824): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-20 21:59:57.102: W/System.err(8824): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-20 21:59:57.102: W/System.err(8824): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-20 21:59:57.102: W/System.err(8824): at java.lang.Thread.run(Thread.java:818)

最佳答案

作为 loopj 的替代方案,您可以创建一个 AsyncTask 来执行程序上的任务。几乎和 loop 一样。我使用下面的类来执行 HTTP/S 请求并且它完成了工作。

class GetHTTPSTask extends AsyncTask<Void, Void, Boolean>
{
private String mUrl;

public GetHTTPSTask(String url)
{
this.mUrl = url;
}

@Override
protected Boolean doInBackground(Void... params)
{
try
{
URL urlConnection = new URL(mUrl);
HttpURLConnection connection = (HttpURLConnection) urlConnection
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();

return Boolean.TRUE;
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Boolean result)
{
if ( result != null)
{
// Connection was successful
// Do something here
}
super.onPostExecute(result);
}
}

并使用它:

new GetHTTPSTask("https://www.google.com/").execute();

关于android - 如何在 Android 的 loopj 中支持 Comodo SSL 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763500/

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