JavaFX中的WebView,不适用于Whatsapp Web [英] WebView in javaFX, not work with Whatsapp web

查看:204
本文介绍了JavaFX中的WebView,不适用于Whatsapp Web的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从没有得到的JavaFX应用程序访问WhatsApp Web.当页面打开时,我收到一条消息,要求使用其他浏览器.我试图更改UserAgent,但是它不起作用.

I need to access the WhatsApp Web from a JavaFX application I'm not getting. When the page opens I get a message to use another browser. I have tried to change the UserAgent but it does not work.

代码:

WebEngine eng = webView.getEngine();
eng.load("https://web.whatsapp.com/");
eng.setJavaScriptEnabled(true);
eng.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36");

我该如何解决?

推荐答案

我无法从JavaFX访问whatsapp,这似乎不是用户代理问题,但是使用FX时缺少某些依赖项,或者只是whatsapp正在使用其他指标阻止,但找到了替代方法; Pandomium

I was unable to access whatsapp from JavaFX, it does not seem to be an user-agent issue, but rather either some dependency is missing when using FX, or just whatsapp is blocking using some other metric, but found an alternative; Pandomium

它与JavaFX兼容性不佳,但与Swing兼容,并且可以访问任何站点而无需考虑用户代理或任何依赖关系问题,因为它是

It is not compatible well with JavaFX, but works well with Swing, and can access to any site without thinking about user-agent, or any dependency issue since it is a JCEF implementation, that is Chromium Framework for Java, essentially embedded Chrome for Java.

只需在依赖项中对Pandomium jar执行以下操作即可;

Just doing the following with Pandomium jar in your dependency does the trick;

public static void main(String[] args) {
    PandomiumSettings settings = PandomiumSettings.getDefaultSettingsBuilder().build();
    settings.getCefSettings().cache_path = "C:\\temp\\cache";
    // setting this option is paramount otherwise no cache is kept, and sesions are lost

    Pandomium pandomium = new Pandomium(settings);
    pandomium.initialize();

    PandomiumClient client = pandomium.createClient();
    PandomiumBrowser browser = client.loadURL("https://web.whatsapp.com");

    JFrame frame = new JFrame();
    frame.getContentPane().add(browser.toAWTComponent(), BorderLayout.CENTER);
    frame.setTitle("Whatsapp");
    frame.setSize(1720, 840);
    frame.setVisible(true);
}

这篇关于JavaFX中的WebView,不适用于Whatsapp Web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆