2013-03-22 64 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瞭解詳情。