6

My browser (webview) starts with an HTML page

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

Whenever I access the google, I want to know whether the browser check, if the network has proxy (proxy'm working manual)

So that the browser shows a dialog to enter User name and password.

Sandy
  • 2,429
  • 7
  • 33
  • 63
Folie
  • 327
  • 3
  • 9

1 Answers1

2

You can use ProxySelector to check proxy. See next example:

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

authentification may require additional code in some cases, see Authenticated HTTP proxy with Java for details.

Community
  • 1
  • 1
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141