如何登录网页并将数据检索到 Java 应用程序?
How to login a Webpage and retrieve data to a Java Application?
我想制作一个应用程序供自己使用。要显示 wimax 信号,
CINR、上传速度等。当设备连接时,打开浏览器
http://192.168.2.1
,登录后(javascript 登录页面)显示
值。
我想将这些数据添加到我的应用程序中。那么,我该怎么做呢?
有例子吗?
您可以使用像 Jsoup 这样的库来解析 HTML 数据。
我假设这是您尝试访问的路由器,它通常使用 basic authentication。您还需要一个库来为基本身份验证进行 base64 编码,例如 Apache commons。
import org.apache.commons.codec.binary.Base64;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
...
public static void main(String[] args) {
String username = "username";
String password = "password";
String login = username + ":" + password;
String base64login = new String(Base64.encodeBase64(login.getBytes()));
Document document = Jsoup
.connect("http://192.168.2.1")
.header("Authorization", "Basic " + base64login)
.get();
Element e = document.select("body");
...
/* Select the elements and HTML text you're interested here */
}
这应该足以让您入门。您可以了解更多关于如何使用 Jsoup here
我想制作一个应用程序供自己使用。要显示 wimax 信号,
CINR、上传速度等。当设备连接时,打开浏览器
http://192.168.2.1
,登录后(javascript 登录页面)显示
值。
我想将这些数据添加到我的应用程序中。那么,我该怎么做呢? 有例子吗?
您可以使用像 Jsoup 这样的库来解析 HTML 数据。
我假设这是您尝试访问的路由器,它通常使用 basic authentication。您还需要一个库来为基本身份验证进行 base64 编码,例如 Apache commons。
import org.apache.commons.codec.binary.Base64;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
...
public static void main(String[] args) {
String username = "username";
String password = "password";
String login = username + ":" + password;
String base64login = new String(Base64.encodeBase64(login.getBytes()));
Document document = Jsoup
.connect("http://192.168.2.1")
.header("Authorization", "Basic " + base64login)
.get();
Element e = document.select("body");
...
/* Select the elements and HTML text you're interested here */
}
这应该足以让您入门。您可以了解更多关于如何使用 Jsoup here