在 JavaFX WebEngine 上设置代理? [英] Set proxy on JavaFX WebEngine?

查看:56
本文介绍了在 JavaFX WebEngine 上设置代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为每个 WebView 实例设置代理?

这是我目前所拥有的:

public void start(Stage stage) {
    StackPane root = new StackPane();

    WebView view = new WebView();
    WebEngine engine = view.getEngine();
    engine.load("https://www.google.com");
    root.getChildren().add(view);

    Scene scene = new Scene(root, 960, 640);
    stage.setScene(scene);
    stage.show();
}

public static void main(String[] args) throws IOException {
    Application.launch(args);
}

这会启动一个带有 google 页面的窗口.

That launches a window with the google page just fine.

但是如何设置代理?不是虚拟机系统代理,而是每个 WebView 窗口的代理.

However how can I set a proxy? Not the VM system proxy, but a proxy per WebView window.

推荐答案

来自 部署概览:

3.2.3 内置代理支持

正确打包的 JavaFX 应用程序具有根据 Java 运行时配置设置初始化的代理设置.默认情况下,这意味着如果应用程序嵌入到网页中,代理设置将从当前浏览器中获取,或者将使用系统代理设置.代理设置在所有执行模式下默认初始化.

Properly packaged JavaFX application have proxy settings initialized according to Java Runtime configuration settings. By default, this means proxy settings will be taken from the current browser if the application is embedded into a web page, or system proxy settings will be used. Proxy settings are initialized by default in all execution modes.

可能无法设置每个 WebView 实例.我想到了一个黑客,但我真的不想这样做 - 扩展 WebView,这样每当用户(以及 WebView 中的脚本等)与它交互时,它就会调用 System.setProperty("http.proxy",this.myProxy).类似的东西:

It might not be possible to set per WebView instance. One hack comes to mind, but I really wouldn't want to do it - extend WebView such that whenever the user (and scripts and such in the WebView) interact with it, it calls System.setProperty("http.proxy",this.myProxy). Something like:

class KludgeWebView extends WebView {
  String myProxy;
  String myProxyPort;
  String sysProxy;
  String sysProxyPort;

  KludgeWebView()
  {
    super();

    sysProxy = System.getProperty("http.proxy");
    sysProxyPort = System.getProperty("http.proxyPort");
  }

  public void load(String url)
  {
    useProxy();
    super.load(url);
    revertProxy();
  }

  public void useProxy()
  {
    System.setProperty("http.proxy",myProxy);
    System.setProperty("http.proxyPort", myProxyPort);
  }

  public void revertProxy()
  {
    System.setProperty("http.proxy",sysProxy);
    System.setProperty("http.proxyPort", sysProxyPort);    
  }
}

然而,这对我来说似乎很混乱.它可能会遗漏诸如用户单击 WebView 内的链接之类的内容,或者执行诸如 XmlHttpRequest 之类的脚本之类的事情.除非您别无选择,否则我不会推荐这个.

However, this seems very messy to me. It might miss things like a user clicking on a link inside the WebView, or scripts doing things like XmlHttpRequest. I wouldn't recommend this unless you are left with no other options.

这篇关于在 JavaFX WebEngine 上设置代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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