Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

java code to find response time of a webpage without using selenium and apache

i have written a code to find the response time of a web page but it always give 0 time .can someone plz complete this code to get the response time

mycode

import java.io.*;
import java.net.URL;
public class NewClass {
public static void main(String[] args) throws IOException {


    long start = System.currentTimeMillis();

    URL myURL = new URL("https://stackoverflow.com/");


    long finish = System.currentTimeMillis();
    long totalTime = finish - start; 

    System.out.println("Total Time for page load - "+totalTime);

 }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can do it with

HttpURLConnection connection = null;
       try {
                URL url = new URL("http://stackoverflow.com/");
                connection = (HttpURLConnection) url.openConnection();

                long start = System.currentTimeMillis();
                String jsonResponse = myInputStreamReader(connection.getInputStream());

                long finish = System.currentTimeMillis();
                long totalTime = finish - start;
                System.out.println("Total Time for page load - " + totalTime);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                connection.disconnect();
            }

    static public String myInputStreamReader(InputStream in) throws IOException {

            StringBuilder sb = null;
            try {
                InputStreamReader reader = new InputStreamReader(in);
                sb = new StringBuilder();
                int c = reader.read();
                while (c != -1) {
                    sb.append((char) c);
                    c = reader.read();
                }
                reader.close();
                return sb.toString();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
            return sb.toString();
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...