有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用java打开完整网页的url

是否有一种使用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();

但这给了我url的源代码。。。那对我没用

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

有人可以帮助我,并建议我一些用java实现的方法

另外,一切都在客户网站上


共 (4) 个答案

  1. # 1 楼答案

    1. 您可以使用Cobra
    2. 您也可以使用plainSwing
    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. # 2 楼答案

    是的,家是对的,这应该会起作用。但这种方法要简单得多。。。尽管有点错误,但它应该可以用于基准测试

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

    如果您只使用此代码段,脚本仍会呈现在屏幕上,尽管可以解决此问题,但我认为您不必为此费心,因为您只需要对加载时间进行基准测试。。。但是,结果可能会略有不同,因为运行JavaScript不需要时间

  3. # 3 楼答案

    要使用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。这里有很多建议

  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();
      }
    }