java - 在 JavaFX WebEngine 上设置代理?

标签 java webview proxy javafx

如何为每个 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 页面的窗口。

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

最佳答案

来自deployment overview :

3.2.3 Built-In Proxy Support

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)。像这样的东西:

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 之类的脚本之类的事情。除非您别无选择,否则我不推荐这样做。

关于java - 在 JavaFX WebEngine 上设置代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691472/

相关文章:

Maven 无法计算构建计划 :Failure to transfer org. apache.maven.plugins

apache - 在 Apache 反向代理后面使用 SSL 的 JIRA 服务器无法正常工作

python - 使用 Django 的代理服务器

java - 如何拥有一个复杂的对象作为@requestBody?

java - generate_series() 函数缺少 PostgreSQL

java - 在 playframework 中打开文件夹选择

javascript - 在 webView 上加载本地存储的文件时出现延迟

java - 了解 hibernate session 初始化

android - 为什么我的 ionic 模式不能在 android 4.4 设备上打开?

Android检测webview URL更改