0
我正在嘗試構建一個網站監控系統。我需要計算網站響應或總加載時間所花費的時間。如何計算java中網站的響應時間
我正在嘗試構建一個網站監控系統。我需要計算網站響應或總加載時間所花費的時間。如何計算java中網站的響應時間
如果要計算http請求和響應之間的往返行程,可以使用apache http components。這曾經被稱爲Apache HTTP客戶端。
下面是我用於提出請求的一些示例代碼。您可以在請求前後抓取時間戳。
HttpClient httpclient = new DefaultHttpClient();
URIBuilder builder = new URIBuilder();
URI uri = null;
HttpEntity entity = null;
InputStream inputStream = null;
JSONObject jsonObject = null;
URL url = this.getClass().getClassLoader().getResource("json" + File.separator + "newInstall.json");
try {
String json = FileUtil.readFile(url.getPath());
builder.setScheme("http").setHost(host).setPort(8080).setPath(basePath + authResource)
.setParameter("sig", "sig").setParameter("params", json);
uri = builder.build();
log.info("uri: {}", uri);
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpclient.execute(httpget);
entity = response.getEntity();
if (entity != null) {
inputStream = entity.getContent();
String jsonString = IOUtils.toString(inputStream, "UTF-8");
jsonObject = new JSONObject(jsonString);
log.info("auth response: {}", jsonString);
}
} catch (Exception e) {
log.error("error", e);
}finally {
if(inputStream != null) {
try {
inputStream.close();
}catch(Exception ex) {
log.error("error closing input stream", ex);
}
}
}
你有什麼試過?你有沒有研究過一些可以幫助你解決這個問題的工具? – 2012-07-21 18:11:50