【发布时间】:2017-02-07 10:38:47
【问题描述】:
网站宕机(超时)如何获取服务器状态?
- 目前,如果网站已启动并正在运行,我的程序会识别服务器的状态。
- 例如,如果网站正常 = 200。
- 如果显示错误页面 = 相关服务器状态。
- 如果网站关闭(超时),我的代码将无法工作,因此不会发送电子邮件。
代码: 获取服务器状态:
public class ServerStatus {
public static int getResponseCode(String urlString) throws MalformedURLException, IOException {
URL url = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setRequestMethod("GET");
huc.connect();
return huc.getResponseCode();
}
}
通过电子邮件发送所获得信息的状态(如果服务器关闭/超时“我的问题”将不起作用):
public void EmailFormatAndDataCapture(ITestResult testResult) throws MalformedURLException, IOException {
if (testResult.getStatus() == ITestResult.FAILURE || testResult.getStatus() == ITestResult.SKIP) {
String tempTime = new SimpleDateFormat("hh.mm.ss").format(new Date());
serverStatusMap.put("\n Time:" + tempTime + " , Test Method: " + testResult.getName() + ", Server Status", ServerStatus.getResponseCode(basePage.getCurrentURL().toString()));
failedSkippedTests.put("\n Time:" + tempTime, " Class name: " + this.getClass().getSimpleName().toString() + ", Test Method Name: " + testResult.getName());
}
}
【问题讨论】:
标签: java selenium selenium-webdriver