Why JavaFX WebView failed to load [Status : FAILED

2019-09-09 19:39发布

问题:

Im really confused why WebView failed to load the following URL:

http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm

http://www.java2s.com/Code/Java/JavaFX/WebEngineLoadListener.htm

After running my code WebEngine's State returns FAILED.

But when I tried to run this URL : https://www.google.com.ph/, WebEngine's State returns SUCCEEDED, and it loaded SUCCESSFULLY!

WHY ????

Is there some kind of CONFIGURATION that I needed to set in order for those sites to load successfully?

Here's the code:

package webviewsample;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage){

        WebView browser = new WebView();
        final WebEngine webEngine = browser.getEngine();
        webEngine.load("http://docs.oracle.com/javafx/2/webview/jfxpub-webview.htm");
        webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
            @Override
            public void changed(ObservableValue<? extends State> ov, State t, State t1) {
                System.out.println(t1);
                if (t1 == Worker.State.SUCCEEDED) {
                    System.out.println("Done!");
                } 
            }
        });

        Button b = new Button("Show Console");
        b.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent t) {
                webEngine.executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");
            }
        });

        VBox root = new VBox(10);
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(10));
        root.getChildren().addAll(browser,b);

        Scene scene = new Scene(root, 700, 550);

        primaryStage.setTitle("WebView Sample");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Please let me know if you got the same result too.

UPDATED:

java.lang.Throwable: Connection reset by server
    at javafx.scene.web.WebEngine$LoadWorker.describeError(WebEngine.java:1436)
    at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(WebEngine.java:1375)
    at javafx.scene.web.WebEngine$LoadWorker.access$1200(WebEngine.java:1253)
    at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(WebEngine.java:1240)
    at com.sun.webkit.WebPage.fireLoadEvent(WebPage.java:2400)
    at com.sun.webkit.WebPage.fwkFireLoadEvent(WebPage.java:2244)
    at com.sun.webkit.network.URLLoader.twkDidFail(Native Method)
    at com.sun.webkit.network.URLLoader.notifyDidFail(URLLoader.java:862)
    at com.sun.webkit.network.URLLoader.lambda$didFail$97(URLLoader.java:845)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)