JavaFx WebView Finding All Requested Urls

JavaFx WebView Finding All Requested Urls

Posted by dogukanhan on January 09, 2021 · 5 mins read

This is an example of finding all requested urls in webview. Reflection used to get the urlstream handler, created custom handler which outputs the requested url and uses the urlhandler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class Test extends Application {


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

    @Override
    public void start(Stage primaryStage) {
        webView(primaryStage);
    }

    public void webView(Stage primaryStage) {
        WebView webView = new WebView();

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();
        URLStreamHandler httpHandler = getURLStreamHandler("http");
        URLStreamHandler httpsHandler = getURLStreamHandler("https");

        URLStreamHandlerFactory streamHandlerFactory = protocol -> {
            URLStreamHandler handler = getURLStreamHandler(protocol);
            if (!(protocol.equals("http") || protocol.equals("https")))
                return handler;
            else {
                return new URLStreamHandler() {
                    @Override
                    protected URLConnection openConnection(URL u) throws IOException {
                        String link = u.toString();
                        System.out.println("link="+link);
                        if(protocol.equals("http")) {
                            return callMethod(u, httpHandler);
                        }else if(protocol.equals("https")){
                            return callMethod(u, httpsHandler);
                        }else{
                            System.out.println("Error!");
                            return null;
                        }
                    }
                };
            }
        };
        URL.setURLStreamHandlerFactory(streamHandlerFactory);
        webView.getEngine().load("http://dogukanhan.com");
        webView.getEngine().locationProperty().addListener((observable, oldValue, newValue) -> {
            System.out.println("Location Change" + oldValue + " " + newValue);
        });
    }

    public static URLStreamHandler getURLStreamHandler(String protocol) {
        try {
            Method method = URL.class.getDeclaredMethod("getURLStreamHandler", String.class);
            method.setAccessible(true);
            return (URLStreamHandler) method.invoke(null, protocol);
        } catch (Exception e) {
            return null;
        }
    }

    public static URLConnection callMethod(URL url, URLStreamHandler httpHandler) {
        try {
            Method method = URLStreamHandler.class.getDeclaredMethod("openConnection", URL.class);
            method.setAccessible(true);
            return (URLConnection) method.invoke(httpHandler, url);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}