How to get the Status of a Server if the Website is Down (Timeout)?
code: Get the server status:
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();
}
}
Email the status of the information gained (Will not work if the server is down / timeout 'My Problem'):
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());
}
}
As stated in getResponseCode() docs it throws IOException - if an error occurred connecting to the server.
https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html#getResponseCode()
You should handle this exception using try catch clause. For example:
try{
// here getStatus
} catch (IOException e){
// handle your timeout scenario
}
Edit:
Probably Exception will be thrown earlier when you try to establish connection so you must choose where you need to catch the Exception to meet your requirements.
I would do something like this:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.