2013-03-22 52 views
5

我的浏览器(web视图)开始与一个HTML页面自动检测代理服务器 - JavaFX的 - 网页视图

FILEJAVA.class.getResource( “FILEHTML.html”)。 ToExternalForm()

每当我访问谷歌,我想知道是否浏览器检查时,如果网络上有代理(proxy'm工作手册)

使浏览器显示一个对话框,输入用户名和密码。

回答

2

您可以使用ProxySelector来检查代理。看下例子:

public class DetectProxy extends Application { 

    private Pane root; 

    @Override 
    public void start(final Stage stage) throws URISyntaxException { 
     root = new VBox(); 

     List<Proxy> proxies = ProxySelector.getDefault().select(new URI("http://google.com")); 
     final Proxy proxy = proxies.get(0); // ignoring multiple proxies to simplify code snippet 
     if (proxy.type() != Proxy.Type.DIRECT) { 
      // you can change that to dialog using separate Stage 
      final TextField login = new TextField("login"); 
      final PasswordField pwd = new PasswordField(); 
      Button btn = new Button("Submit"); 
      btn.setOnAction(new EventHandler<ActionEvent>() { 
       @Override 
       public void handle(ActionEvent t) { 
        System.setProperty("http.proxyUser", login.getText()); 
        System.setProperty("http.proxyPassword", pwd.getText()); 
        showWebView(); 
       } 
      }); 
      root.getChildren().addAll(login, pwd, btn); 
     } else { 
      showWebView(); 
     } 

     stage.setScene(new Scene(root, 600, 600)); 
     stage.show(); 
    } 

    private void showWebView() { 
     root.getChildren().clear(); 
     WebView webView = new WebView(); 

     final WebEngine webEngine = webView.getEngine(); 
     root.getChildren().addAll(webView); 
     webEngine.load("http://google.com"); 

    } 

    public static void main(String[] args) { 
     launch(); 
    } 
} 

认证可能需要额外的代码,在某些情况下,看到Authenticated HTTP proxy with Java了解详情。