2013-03-22 32 views
5

Trình duyệt của tôi (webview) bắt đầu bằng một trang HTMLProxy tự động phát hiện - JavaFX - webview

FILEJAVA.class.getResource ("FILEHTML.html"). ToExternalForm()

Bất cứ khi nào tôi truy cập vào google, tôi muốn biết liệu việc kiểm tra trình duyệt, nếu mạng có proxy (proxy'm thủ công làm việc)

Vì vậy mà trình duyệt cho thấy một hộp thoại để nhập tên người dùng và mật khẩu.

Trả lời

2

Bạn có thể sử dụng ProxySelector để kiểm tra proxy. Xem ví dụ tiếp theo:

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(); 
    } 
} 

xác thực có thể yêu cầu mã bổ sung trong một số trường hợp, xem Authenticated HTTP proxy with Java để biết chi tiết.

Các vấn đề liên quan