gpt4 book ai didi

java - 使用 SSL 解析网站 (Android)

转载 作者:行者123 更新时间:2023-12-01 15:45:55 25 4
gpt4 key购买 nike

我的目标是能够登录到 my3 网站来解析数据,但目前,在尝试拉到登录页面时出现“javax.net.ssl.SSLException:不受信任的服务器证书”错误。

我使用了 here 中的示例源代码它应该忽略所有 SSL 证书,但我仍然收到相同的错误。

代码:

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

DefaultHttpClient client = new DefaultHttpClient();

SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

try {

final String url = "https://sso.three.co.uk/mylogin/?service=https%3A%2F%2Fwww.three.co.uk%2FThreePortal%2Fappmanager%2FThree%2FSelfcareUk%3F_pageLabel%3DP22200581501288714397101%26_nfpb%3Dtrue%26&resource=portlet";
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost);

} catch(IOException e) {

AlertDialog alertDialog = new AlertDialog.Builder(SslActivity.this).create();
alertDialog.setTitle("About");
alertDialog.setMessage(e.toString());
alertDialog.setButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

}
});

alertDialog.show();

}

最佳答案

Android 上的 SSL 是一种痛苦。

我为一个库 (GreenDroid) 编写了一个类,它创建了一个 HttpClient,它允许毫无问题地使用 HTTPS。

https://github.com/kennydude/GreenDroid/blob/master/GreenDroid/src/greendroid/util/HttpClientHelper.java

:)

关于java - 使用 SSL 解析网站 (Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011283/

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