Search code examples
javajavafxjavafx-webengine

JavaFX detecting 404 responses from WebEngine


Using JavaFx WebEngine, how can I detect if there is a 404 page not found error. I have searched but not able to find anything. Any ideas? or Is it possible?


Solution

  • Most likely you would have to add your own URL handler (like in this post javafx 2 webview custom url handler, don't work relative url) and retrieve/detect 404 messages and conditions yourself.

    This article (How to check if a URL exists or returns 404 with Java?) shows you how to detect a 404 condition.

    In the handler you have 2 choices, either create a HEAD request for a URL (and subsequently ignore HEAD requests so you don't get yourself into an infinite loop) or masquerade the returned URLConnection after checking for a 404.

    @Override
    protected URLConnection openConnection(URL u) throws IOException {
        // 1. Use Apache HTTPClient or something else to grab the url
        // 2. Read the resultCode and report if it's a 404
        // 3. Return an object that subclasses URLConnection
    }