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

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

问题描述

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

How can I set a proxy per WebView instance?

这是我到目前为止所做的:

This is what I have so far:

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

启动谷歌页面的窗口就好了。

That launches a window with the google page just fine.

但是如何设置代理? 不是VM系统代理,而是每个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实例设置。我想到了一个hack,但我真的不想这样做 - 扩展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天全站免登陆