我做了一个方法来检查http服务器是否响应正常。我正在使用 apache 组件。
private static int CheckAvailabilityHttp(String pHostName, int pPort)
{
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(String.format("http://%s:%s",pHostName,pPort));
HttpResponse response = httpClient.execute(httpGet);
int status = response.getStatusLine().getStatusCode();
if (HttpStatus.SC_OK == status) { // HttpStatus.SC_OK is 200 and status is 200
return ServerStatuses.SUCCESS;
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return ServerStatuses.FAILED;
}
问题出在比较线上:
if (HttpStatus.SC_OK == status)
即使 BOTH(HttpStatus.SC_OK 和状态变量)都是 200,它也不会进入条件范围内......它甚至不会引发异常。
我做错了什么??
感谢您的时间!