如果网站关闭(超时),如何获取服务器的状态?

时间:2017-02-07 10:38:47

标签: java selenium selenium-webdriver

如果网站关闭(超时),如何获取服务器的状态?

  1. 目前,我的程序会在网站启动并运行时识别服务器的状态。
  2. 例如,如果网站没问题= 200。
  3. 如果显示的页面错误=相关服务器状态。
  4. 如果网站关闭(超时),我的代码dosnt工作,因此没有发送电子邮件。
  5. 代码: 获取服务器状态:

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

    enter image description here

2 个答案:

答案 0 :(得分:2)

如getResponseCode()docs中所述,它会抛出IOException - 如果连接到服务器时发生错误。

https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html#getResponseCode()

您应该使用try catch子句处理此异常。例如:

try{
   // here getStatus
} catch (IOException e){
   // handle your timeout scenario
}

修改

当您尝试建立连接时,可能会提前抛出异常,因此您必须选择捕获异常的位置以满足您的要求。

答案 1 :(得分:0)

我会做这样的事情:

CODE

public class ServerStatus {
    private static final int INVALID_HTTP_RESPONSE = -1;
    private static final int MALFORMED_URL = -999;
    private static final int STATUS_CHECK_TIMEOUT = -998;
    private static final int UNEXPECTED_IO_EXCEPTION = -997;

    private static Throwable lastException=null;

    private static void setLastException(Throwable t) {
        lastException=t;
    }

    public static String getLastStatusDetails() {
        StringBuilder statusDetails= new StringBuilder();

        if (lastException!=null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);

            lastException.printStackTrace(pw);

            String lastExceptionStackTrace = sw.toString();
            statusDetails.append(lastExceptionStackTrace);
        }

        // You may append other useful information to the details...
        return statusDetails.toString();
    }

    public static int getResponseCode(String urlString) {
        int ret;

        try {
            URL url = new URL(urlString);
            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            huc.setRequestMethod("GET");
            huc.connect();
            ret= huc.getResponseCode();
        } catch(MalformedURLException mue) {
            setLastException(mue);
            ret=MALFORMED_URL;
        } catch (SocketTimeoutException ste) {
            setLastException(ste);
            ret=STATUS_CHECK_TIMEOUT;
        } catch(IOException ioe) {
            setLastException(ioe);
            ret=UNEXPECTED_IO_EXCEPTION ;
        }

        return ret;
    }
}

示例使用

 int responseCode = ServerStatus.getResponseCode("http://example.com/service");

 if (responseCode < 0) {
     switch(responseCode) {
         case STATUS_CHECK_TIMEOUT:
             sendMail("From", "To", "Subject: Server down", "Body: " + ServerStatus.getLastStatusDetails());
         break;

         // Handle other response codes here...

         default:
            throw new RuntimeException("Unexpected response code: " + responseCode);
     }
 }