2

有没有办法使用 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应用程序来记录网页加载到我的桌​​面所花费的时间。

一切都在客户网站上!

4

4 回答 4

4

这是一个基于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();
  }
}
于 2012-05-04T18:51:07.003 回答
3
  1. 你可以使用眼镜蛇
  2. 你也可以使用普通的 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();
        }
    }
    
于 2012-05-04T07:46:39.060 回答
3

Yeah home is right that should do the trick. But this way is much more simpler... Although a bit erroneous it should serve fine for benchmark purposes.

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

The scripts are still rendered on the screen if you just use this snippet, although it can be worked around I don't think you will need to bother with that since you need to just benchmark loading time... However the results may be varying very slightly since it doesn't take time to run the JavaScript.

于 2012-05-04T07:53:57.017 回答
-1

要使用 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)
  {
   }
  }
  }

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

于 2012-05-04T07:57:21.147 回答