java - 有什么方法可以从 JEditorPane 上显示的网站获取响应?
问题描述
我想从独立应用程序进行身份验证。因此,我将我公司的实习生登录页面显示在JEditorPane
. 首先,您会看到一个像常见的登录窗口。您输入用户名和密码。如果登录成功,页面将变为“登录成功”。我如何区分这些窗口并获取第二个窗口的 URL?
JEditorPane loginPane = new JEditorPane();
loginPane.setEditable(false);
try {
loginPane.setPage("http://mypage.blalala.com");
} catch (IOException e) {
loginPane.setContentType("text/html");
loginPane.setText("<html>Could not load</html>");
}
JScrollPane scrollPane = new JScrollPane(loginPane);
JFrame f = new JFrame("Test HTML");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(scrollPane);
f.setPreferredSize(new Dimension(800, 600));
f.setVisible(true);
解决方案
我认为您需要听众,这是我的示例应用程序
/*
* This code is under license Creative Commons Attribution-ShareAlike 1.0
* <a href="https://creativecommons.org/licenses/by-sa/1.0/legalcode"></a>
*/
package javaapplication5;
import java.awt.Dimension;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
public class DemoAppWebSwing extends JFrame {
private static final DemoAppWebSwing SINGLETON = new DemoAppWebSwing();
public void init() {
JEditorPane loginPane = new JEditorPane();
loginPane.setEditable(false);
try {
loginPane.setPage("https://github.com/vincenzopalazzo");
loginPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane pane = (JEditorPane) e.getSource();
if (e instanceof HTMLFrameHyperlinkEvent) {
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
HTMLDocument doc = (HTMLDocument) pane.getDocument();
doc.processHTMLFrameHyperlinkEvent(evt);
} else {
try {
pane.setPage(e.getURL());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
});
} catch (IOException e) {
loginPane.setContentType("text/html");
loginPane.setText("<html>Could not load</html>");
}
JScrollPane scrollPane = new JScrollPane(loginPane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(scrollPane);
this.setPreferredSize(new Dimension(800, 600));
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SINGLETON.init();
}
});
}
}
使用 HyperlinkListener,您可以在单击链接时获得另一个页面 这是我的侦听器实现:
loginPane.setPage("https://jsp-password-checking-unibas.herokuapp.com");
loginPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane pane = (JEditorPane) e.getSource();
if (e instanceof HTMLFrameHyperlinkEvent) {
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
HTMLDocument doc = (HTMLDocument) pane.getDocument();
doc.processHTMLFrameHyperlinkEvent(evt);
} else {
try {
pane.setPage(e.getURL());
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
})
在此答案的评论部分与另一位用户讨论时,我们得出的结论是JEditorPane
具有一定的局限性。但是,我寻找了另一种将网页导入基于 Java 的 UI 的解决方案。您可以使用JavaFX
.
这是一个基本示例,JavaFX
它与前面显示的示例基本相同,使用Swing
.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class LoginScreen extends Application {
@Override
public void start(final Stage stage) {
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("https://jsp-password-checking-unibas.herokuapp.com");
Scene scene = new Scene(browser, 800, 600);
stage.setTitle("Login Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
推荐阅读
- android - 会话 ID 在来自服务器的 android 中为空
- gitlab - 无法使用 LDAP 在 HTTPS 中克隆,错误 500
- python - OpenCV 图像校正以填充图像中的空白和孔洞
- node.js - 无法使用对话流实现将数据添加到 Firestore
- python - 如何将字符串值分配给numpy数组中的特定行和列?
- django - 解决 Docker 中的 pipenv install weasyprint 错误
- azure-ad-b2c - Azure ADB2C 中的电话注册和登录自定义策略不起作用
- python - 静态 css 文件未在 Django 管理员中加载
- javascript - Firebase 实时数据库:如何获取父节点的值
- kdb - 在 kdb+ 中选择和索引访问表的翻转结果不同