JS:此方法在IE下面不行
function getURL(url){
var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET",url,false); xmlhttp.send();
if(xmlhttp.readyState==4){
if(xmlhttp.Status!=200){ alert("URL地址不存在!"); }else{window.location.href=url; }
}
}
java:
- /**
- * 功能:檢測當前URL是否可連接或是否有效,
- * 描述:最多連接網絡 3 次, 如果 3 次都不成功,視爲該地址不可用
- * @param urlStr 指定URL網絡地址
- * @return URL
- */
- public synchronized String isConnect(String urlStr) {
- int counts = 0;
- retu = "";
- if (urlStr == null || urlStr.length() <= 0) {
- return null;
- }
- while (counts < StaticConstant.REQUEST_COUNT) {
- long start = 0;
- try {
- url = new URL(urlStr);
- start = System.currentTimeMillis();
- con = (HttpURLConnection) url.openConnection();
- state = con.getResponseCode();
- log.info("請求斷開的URL一次需要:"+(System.currentTimeMillis()-start)+"毫秒");
- if (state == 200) {
- retu = "ok";
- log.info(urlStr+"--可用");
- }
- break;
- }catch (Exception ex) {
- counts++;
- log.info("請求斷開的URL一次需要:"+(System.currentTimeMillis()-start)+"毫秒");
- log.info("連接第 "+counts+" 次,"+urlStr+"--不可用");
- continue;
- }
- }
- return retu;
- }