在使用Java进行HTTP请求期间,我需要分别测量以下时间:
目前,我只能测量连接时间以及请求和响应时间:
URL url = new URL("https://stackoverflow.com/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
Instant time1 = Instant.now();
connection.connect();
Instant time2 = Instant.now();
System.out.println(connection.getResponseCode());
Instant time3 = Instant.now();
InputStream stream = connection.getInputStream();
stream.readAllBytes();
Instant time4 = Instant.now();
System.out.printf("Connection: %d ms\n", Duration.between(time1, time2).toMillis());
System.out.printf("Request/Receive Headers : %d ms\n", Duration.between(time2, time3).toMillis());
System.out.printf("Receive Body : %d ms\n", Duration.between(time3, time4).toMillis());
//输出:
如何分别计算“请求”时间?