Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaFXWebEngine上设置代理?_Java_Webview_Proxy_Javafx - Fatal编程技术网

在JavaFXWebEngine上设置代理?

在JavaFXWebEngine上设置代理?,java,webview,proxy,javafx,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(

如何为每个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);
}
这将启动一个带有谷歌页面的窗口

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

3.2.3内置代理支持

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

可能无法设置每个WebView实例。我想到了一个黑客,但我真的不想这么做——扩展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之类操作的脚本。除非您没有其他选择,否则我不推荐使用此选项。

http.proxy不起作用,我必须使用http.proxyHost

System.setProperty("http.proxyHost","proxy.esrf.fr");
System.setProperty("http.proxyPort","3128");

我已经尝试了上面所有的答案,但没有一个对我有效。我还尝试使用更改系统设置

System.setProperty("http.proxyHost","your proxy address");
System.setProperty("http.proxyPort","your port");
但这也没用。我发现唯一可行的解决方案是让Java应用程序运行命令

Runtime.getRuntime().exec(CHANGE_PROXY_CMD);
这将通过修改注册表设置来更改系统代理设置。无论如何,都不可能按照javafx文档中明确说明的那样为每个web实例设置代理

有三个命令

  • 启用代理服务器设置
  • 更改代理服务器设置
  • 禁用代理服务器设置
就这样

public void changeProxySettings(String ip, String port) {

    StringBuffer output = new StringBuffer();

    System.setProperty("java.net.useSystemProxies", "true");

    String ENABLE_PROXY_CMD = " reg add \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" \n" + "    /v ProxyEnable /t REG_DWORD /d 1 /f";

    String CHANGE_PROXY_CMD = "reg add \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" \n" + "    /v ProxyServer /t REG_SZ /d " + ip + ":" + port + "  /f";

    String DISABLE_PROXY_CMD = "reg add \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\" \n" + "    /v ProxyEnable /t REG_DWORD /d 0 /f";

    Process processEnableProxy, processChangeProxy, processDisableProxy;
    try {
        processEnableProxy = Runtime.getRuntime().exec(ENABLE_PROXY_CMD);

        processEnableProxy.waitFor();//makes the current thread to wait until system settings are applied

        processChangeProxy = Runtime.getRuntime().exec(CHANGE_PROXY_CMD);

        processChangeProxy.waitFor();

        BufferedReader reader = new BufferedReader(new InputStreamReader(processEnableProxy.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    System.out.println(output.toString());

}
你应该使用

System.setProperty(“http.proxyHost”、“您的代理地址”);
setProperty(“http.proxyPort”、“您的端口”)

对于http站点和

System.setProperty(“https.proxyHost”,“您的代理地址”);
System.setProperty(“https.proxyPort”、“您的端口”)


对于https站点

有人吗?关于这方面的文档很少。