【问题标题】:How to get the Status of a Server if the Website is Down (Timeout)?如果网站关闭(超时),如何获取服务器的状态?
【发布时间】:2017-02-07 10:38:47
【问题描述】:

网站宕机(超时)如何获取服务器状态?

  1. 目前,如果网站已启动并正在运行,我的程序会识别服务器的状态。
  2. 例如,如果网站正常 = 200。
  3. 如果显示错误页面 = 相关服务器状态。
  4. 如果网站关闭(超时),我的代码将无法工作,因此不会发送电子邮件。

代码: 获取服务器状态:

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


    【解决方案1】:

    如 getResponseCode() 文档中所述,它会抛出 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
    }
    

    编辑:

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

    【讨论】:

    • 感谢您的帮助,如果我的方法是 int 类型并且我需要它是 int 类型以便通过电子邮件发送,我将如何返回异常
    • 您应该了解异常在 Java 中的工作原理。它与您的返回类型无关。您可以在此处阅读有关异常的更多信息:docs.oracle.com/javase/tutorial/essential/exceptions
    • 感谢我不确定是否可以将异常作为 int 类型返回并使用上述代码附加到我的电子邮件>?
    • 无法将 Exception 作为 int 返回,但可以在 catch 子句中返回 int。由您决定如何实现您的 catch 子句,以及您将在哪种方法上捕获异常。
    【解决方案2】:

    我会这样做:

    代码

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2017-12-08
      • 1970-01-01
      相关资源
      最近更新 更多