【问题标题】:Opening a full web page打开一个完整的网页
【发布时间】:2012-05-13 18:08:10
【问题描述】:

有没有使用Java打开完整网页的方法,我必须检查在Java用户端应用程序中打开完整网页所花费的时间,我已经尝试过这段代码:

    URL ur = new URL("http://www.google.com/");
    HttpURLConnection yc =(HttpURLConnection) ur.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
    System.out.println(inputLine);
    in.close();

但这给了我网址的源代码……这对我没用!

因为我需要通过制作 Java 应用程序来记录网页加载到我的桌​​面所花费的时间。

一切都在客户网站上!

【问题讨论】:

  • 您想将它加载到正确渲染的 GUI 组件中吗?
  • @home: 让我试试你的帖子...
  • @Thihara:是的,我需要加载网页的完整 GUI,不需要显示它......只是想捕捉打开完整 GUI 所花费的时间......
  • @rahul 在下面看到我的答案。它只是基于 Swing GUI 组件。虽然不是完美的渲染,但它应该可以满足您的目的。

标签: java url client-side


【解决方案1】:
  1. 您可以使用Cobra
  2. 您也可以使用plain Swing
  3. 你也可以使用 JavaFX 来做这样的事情:

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    import javafx.scene.web.WebView;
    
    public class MyWebView extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(final Stage primaryStage) {
            final WebView wv = new WebView();
            wv.getEngine().load("http://www.google.com");
            primaryStage.setScene(new Scene(wv));
            primaryStage.show();
        }
    }
    

【讨论】:

    【解决方案2】:

    这是一个基于JavaFX 的示例:

    import java.util.Date;
    import javafx.application.Application;
    import javafx.beans.value.*;
    import javafx.concurrent.Worker.State;
    import javafx.event.*;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.image.Image;
    import javafx.scene.layout.*;
    import javafx.scene.web.WebEngine;
    import javafx.stage.Stage;
    
    /** times the loading of a page in a WebEngine */
    public class PageLoadTiming extends Application {
      public static void main(String[] args) { launch(args); }
      @Override public void start(Stage stage) {
        final Label instructions = new Label(
          "Loads a web page into a JavaFX WebEngine.  " +
          "The first page loaded will take a bit longer than subsequent page loads due to WebEngine intialization.\n" +
          "Once a given url has been loaded, subsequent loads of the same url will be quick as url resources have been cached on the client."
        );
        instructions.setWrapText(true);
        instructions.setStyle("-fx-font-size: 14px");
    
        // configure some controls.
        final WebEngine engine   = new WebEngine();
        final TextField location = new TextField("http://docs.oracle.com/javafx/2/get_started/jfxpub-get_started.htm");
        final Button    go       = new Button("Go");
        final Date      start    = new Date();
        go.setOnAction(new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent arg0) {
            engine.load(location.getText());
            start.setTime(new Date().getTime());
          }
        });
        go.setDefaultButton(true);
        final Label timeTaken = new Label();
    
        // configure help tooltips.
        go.setTooltip(new Tooltip("Start timing the load of a page at the entered location."));
        location.setTooltip(new Tooltip("Enter the location whose page loading is to be timed."));
        timeTaken.setTooltip(new Tooltip("Current loading state and time taken to load the last page."));
    
        // monitor the page load status and update the time taken appropriately.
        engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
          @Override public void changed(ObservableValue<? extends State> state, State oldState, State newState) {
            switch (newState) {
              case SUCCEEDED: timeTaken.setText(((new Date().getTime()) - start.getTime()) + "ms"); break;
              default: timeTaken.setText(newState.toString());  
            }
          }
        });
    
        // layout the controls.
        HBox controls = HBoxBuilder.create().spacing(10).children(new Label("Location"), location, go, timeTaken).build();
        HBox.setHgrow(location, Priority.ALWAYS);
        timeTaken.setMinWidth(120);
    
        // layout the scene.
        VBox layout = new VBox(10);
        layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk; -fx-font-size: 20px;");
        layout.getChildren().addAll(controls, instructions);
        stage.setTitle("Page load timing");
        stage.getIcons().add(new Image("http://icons.iconarchive.com/icons/aha-soft/perfect-time/48/Hourglass-icon.png"));
        stage.setScene(new Scene(layout, 1000, 110));
        stage.show();
    
        // trigger loading the default page.
        go.fire();
      }
    }
    

    【讨论】:

      【解决方案3】:

      是的,家是正确的,应该可以解决问题。但是这种方式要简单得多......虽然有点错误,但它应该可以很好地用于基准测试。

      JEditorPane editorPane = new JEditorPane();
      editorPane.setPage(new URL("http://www.google.com"));
      

      如果你只使用这个 sn-p,脚本仍然会在屏幕上呈现,尽管它可以解决我认为你不需要为此烦恼,因为你只需要对加载时间进行基准测试......但是结果可能会略有不同,因为运行 JavaScript 并不需要时间。

      【讨论】:

      • 是的,它工作得很好!!但是唉,java脚本也是必不可少的一部分!!一些网站完全依赖这部分!!我需要完美,因为这是一个项目的一部分,毫秒确实很重要!
      • 你说它是“错误的”...你能告诉我一些错误或异常会妨碍我吗??
      • 好吧,JavaScript 主要是呈现给所有人看,因为您在 JEditorPane 中没有 JavaScript 引擎,所以它不会运行 JavaScript...
      • 可能重复。 stackoverflow.com/questions/817609/… 你可能不得不为 JavaScript 的东西获取第三方库。你为什么不想使用库呢??
      • 你误会了,第三方库没问题,我是说我不能使用任何其他软件来处理这些东西(例如资源管理器)!!
      【解决方案4】:

      要使用 java 打开网页,请尝试以下代码:

       import java.io.*;
        public class b {
       public static void main(String args[])throws IOException
       {
       String downloadURL="<url>";
          java.awt.Desktop myNewBrowserDesktop = java.awt.Desktop.getDesktop();
       try
        {
             java.net.URI myNewLocation = new java.net.URI(downloadURL);
            myNewBrowserDesktop.browse(myNewLocation);
        }
       catch(Exception e)
        {
         }
        }
        }
      

      这将在您的默认浏览器中打开网页。至于加载零件所用的时间,请看here。那里有很多建议。

      【讨论】:

      • 我需要开发一个应用程序来捕捉时间,我不能使用任何其他第三方应用程序,在浏览器中打开网页不是我的一杯茶!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      相关资源
      最近更新 更多