如何通过 JSoup 轻松使用 HTTPS(自签名证书)?
在大学里,我们必须开发一个Android应用程序,它可以从网站上获取我们的分数。
我们使用 JSoup 框架来抓取和解析 HTML 页面。现在的问题是我们的大学使用自签名证书进行 HTTPS 连接,该证书不受更大的可信 CA 的信任。
对于这些经常出现的问题,我已经看到了一些通用的解决方案,但它们使用带有自己的 SSLSocketFactory 的扩展 DefaultHttpClient。这些解决方案看起来不太好,是否有一个使用 JSoup 框架的简单漂亮的解决方案?
private LoginState connect(String username, String password) {
try {
if (isOnline()) {
Log.i(TAG, "Sending POST.");
Connection connection = Jsoup.connect(LOGIN_URL)
.data(USER_FIELD, username).data(PASS_FIELD, password)
.data(LOGIN_BUTTON, "Anmelden")
.data(LOGIN_TYPE, "login").data(PID, "2")
.timeout(DEFAULT_TIMEOUT);
mDocument = connection.post();
mCookies.putAll(connection.response().cookies());
if (isConnected()) {
Log.i(TAG, "Login successful.");
setState(LoginState.LOGIN_SUCCESSFUL);
} else {
Log.i(TAG, "Login failed.");
setState(LoginState.LOGIN_FAILED);
}
} else {
Log.i(TAG, "No Internet Connection.");
setState(LoginState.BAD_CONNECTION);
}
} catch (IOException ex) {
Log.e(TAG, ex.getStackTrace().toString());
setState(LoginState.BAD_CONNECTION);
}
return getState();
}
In the university we must develop an android application, which grabs our marks from the website.
We use the JSoup Framework to grab and parse the HTML pages. The problem now is that our University uses a self-signed certificate for HTTPS connection, which is not trusted by a larger trusted CA.
I've seen for these frequently occurring problems, some general solutions, but they use a extended DefaultHttpClient with a own SSLSocketFactory. These solutions do not look very nice, is there a simple beautiful solution for this by usage of the JSoup Framework?
private LoginState connect(String username, String password) {
try {
if (isOnline()) {
Log.i(TAG, "Sending POST.");
Connection connection = Jsoup.connect(LOGIN_URL)
.data(USER_FIELD, username).data(PASS_FIELD, password)
.data(LOGIN_BUTTON, "Anmelden")
.data(LOGIN_TYPE, "login").data(PID, "2")
.timeout(DEFAULT_TIMEOUT);
mDocument = connection.post();
mCookies.putAll(connection.response().cookies());
if (isConnected()) {
Log.i(TAG, "Login successful.");
setState(LoginState.LOGIN_SUCCESSFUL);
} else {
Log.i(TAG, "Login failed.");
setState(LoginState.LOGIN_FAILED);
}
} else {
Log.i(TAG, "No Internet Connection.");
setState(LoginState.BAD_CONNECTION);
}
} catch (IOException ex) {
Log.e(TAG, ex.getStackTrace().toString());
setState(LoginState.BAD_CONNECTION);
}
return getState();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非 JSoup 公开一些允许您传递自己的信任存储的 API,否则不会。如果您有 root 设备,则可以使用此将大学的证书安装到信任存储区。如果您有 Android 4.0 (ICS) 设备,则有一个用于安装受信任 CA 证书的 UI。另一个想法:使用 HttpClient 获取数据,然后使用 JSoup 仅进行解析(这应该是可能的)。
Unless JSoup exposes some API that lets your pass your own trust store, no. If you have a rooted device you can install the university's cert to the trust store using this. If you have an Android 4.0 (ICS) device, there is an UI for installing trusted CA certificates. Another idea: use HttpClient to get the data, and then use JSoup for parsing only (that should be possible).