如何为每个 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 窗口的代理。
最佳答案
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/