2012-05-04 36 views
2

是否有一种使用java打开完整网页的方法,我必须检查在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应用程序记录由一个网页所花费的时间我的桌面上加载..

有人plz帮助我,建议我用Java做的一些方法!

P.S.一切都在客户端网站上!

+0

你检查:http://stackoverflow.com/questions/1790500/render-html-in-swing-application – home

+0

要加载它的GUI组件的正确呈现? – Thihara

+0

@home:只是让我试试看你的帖子... – rahul

回答

3

呀家是正确的,应该做的伎俩。但这种方式要简单得多...虽然有点错误,但它应该适用于基准测试。

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

脚本仍呈现在屏幕上,如果你只是用这个片段中,尽管它可以围绕工作,我不认为你需要与打扰,因为你只是需要基准加载时间.. 。但是结果可能会稍有不同,因为它不需要时间来运行JavaScript。

+0

是啊,它的工作完美!但唉Java脚本也是一个必不可少的部分!一些网站完全依赖于这部分!我需要完美,因为这是Milli秒很重要的项目的一部分! – rahul

+0

你说它的'错误的'...你可以启发我关于wt类型的错误或异常可以以我的方式? – rahul

+0

好的JavaScript大部分都会呈现给大家看,而且由于在JEditorPane中没有JavaScript引擎,它不会运行JavaScript ... – Thihara

2
  1. 您可以使用Cobra
  2. 您可以使用plain摇摆也
  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(); 
        } 
    } 
    
-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) 
    { 
    } 
    } 
    } 

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

+0

我需要开发一个应用程序来捕捉时间,我不能使用任何其他第三方应用程序,在浏览器中打开网页不是我的一杯茶! – rahul

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

您可以使用URI类在浏览器中打开该网页。这里是你可以使用的示例代码。

package com.harshit.demo; 
import java.awt.Desktop; 
import java.io.File; 
import java.io.IOException; 
import java.net.URI; 

public class OpenWebsite { 

public static void main(String[] args) { 
try { 
URI uri= new URI("http://amzn.to/2nBhZB4"); 
java.awt.Desktop.getDesktop().browse(uri); 
System.out.println("Amazon opened in browser"); 

} catch (Exception e) { 
e.printStackTrace(); 
     } 
    } 
}