Description: Using java js or visit a Web site, return status code
1.java achieve
// 用getResponseCode可以获取URL返回状态码 String surl = ""; try { surl="你的url"; URL url = new URL(surl); URLConnection rulConnection = url.openConnection(); HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection; httpUrlConnection.setConnectTimeout(300000); httpUrlConnection.setReadTimeout(300000); httpUrlConnection.connect(); String code = new Integer(httpUrlConnection.getResponseCode()).toString(); String message = httpUrlConnection.getResponseMessage(); System.out.println("getResponseCode code ="+ code); System.out.println("getResponseMessage message ="+ message); if(!code.startsWith("2")){ throw new Exception("ResponseCode is not begin with 2,code="+code); } System.out.println(getCurDateTime()+"连接"+surl+"正常"); }catch(Exception ex){ System.out.println(ex.getMessage()); }
2.js achieve (success will return 200, if the page returns a 404 not found)
GetHttpStatusCode function ($ URL) {
$ curl = curl_init ();
curl_setopt ($ curl, CURLOPT_URL to, $ URL); // Get content URL
curl_setopt ($ curl, CURLOPT_HEADER,. 1); // http header information obtaining
curl_setopt ($ curl , CURLOPT_NOBODY, 1); // message body of html not return
curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // returns the data stream is not directly output
curl_setopt ($ curl, CURLOPT_TIMEOUT, 30 ); // timeout time in seconds
the curl_exec ($ curl);
$ RTN = curl_getinfo ($ curl, CURLINFO_HTTP_CODE);
curl_close ($ curl);
return $ RTN;
}
$ URL = "http://www.baidu.com";
GetHttpStatusCode ($ URL);