5

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?

A Paul
  • 8,113
  • 3
  • 31
  • 61

1 Answers1

3

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
}
Community
  • 1
  • 1
disrvptor
  • 1,592
  • 12
  • 23
  • Thanks for the answer. I will check and update here. – A Paul Feb 13 '14 at 12:26
  • 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. – Kevin Wright Aug 02 '18 at 14:15