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?
1 Answer
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
}
2 Comments
A Paul
Thanks for the answer. I will check and update here.
Kevin Wright
What a horrible solution! Changing a write-once JVM-global property that's invasive enough to screw up the entire classloading mechanism... just to be able to read a status code.