【问题标题】:Is there any way to get a Response from a Website displayed on JEditorPane?有什么方法可以从 JEditorPane 上显示的网站获取响应?
【发布时间】:2019-12-27 13:13:52
【问题描述】:

我想从独立应用程序进行身份验证。因此,我在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);

【问题讨论】:

  • 也许here的答案会给你一个想法。
  • @second 不,这不是答案,它只返回本网站上的组件。我想在成功登录的情况下得到网站的回复
  • 我不确定你想说什么。如果您使用HttpClient,您可以向您的服务器发送任何类型的请求,您应该会得到正确的响应。您可能想要编辑您的问题并描述您期望服务器提供什么样的响应。
  • 我认为来自@vincenzopalazzo 的答案应该向您展示一般用例,但它也不适用于我的示例页面。它可能需要一些调整。理论上,如果您正确输入数据,在HyperLinkListener 中,您应该能够向您的服务器生成正确的请求,它应该使用新的 html 页面进行回复。
  • 如果您仍然需要在应用程序内部进行区分,您可能必须解析 url / 或内容以查看您所在的页面(这又会导致初始链接的答案)。

标签: java swing jeditorpane


【解决方案1】:

我认为您需要监听器,这是我的示例应用程序

/*
 * 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 执行此操作。

查看来自oracle 和/或this post 的文档链接以供参考。

这是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);
    }
}

【讨论】:

  • 对不起,但这对我没有帮助。所以我在上面解释更多 Precisley。
  • 我在我的一个简单的 html 页面上尝试了这个解决方案,但我遇到了一些问题。对于我的按钮,我只会收到“正常”HyperlinkEvent 而不是HTMLFrameHyperlinkEvent。虽然我可以从中提取 url,但我的服务器实际上需要一个 post 请求,其中包括来自页面字段的数据。 JEdi​​torPane 似乎也完全忽略了css。也许您可以使用这些信息更新您的示例。
  • 对于 JEditorPane 中的 css 问题,我认为这个答案是一个很好的reference,我已经尝试使用我的 Web 应用程序登录和请求发布工作的代码,我会更新的我的网络应用示例
  • 它可能对html 页面/form 必须如何格式化以使其正常工作有一些要求,这意味着它并不适合所有情况(特别是如果您不能影响页面内容)。
  • 是的,但我认为 JEditorPane 这是一个独特的解决方案,但在我的问题中,我添加了其他解决方案的参考
猜你喜欢
  • 2019-04-03
  • 1970-01-01
  • 2012-05-14
  • 1970-01-01
  • 2020-04-11
  • 2022-06-12
  • 2010-10-28
  • 1970-01-01
  • 2021-07-28
相关资源
最近更新 更多