如何基于java或js獲取URL返回狀態(tài)碼
這篇文章主要介紹了如何基于java或js獲取URL返回狀態(tài)碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
描述:使用java或者js訪問某個網(wǎng)站,返回狀態(tài)碼
1.java實現(xiàn)
// 用getResponseCode可以獲取URL返回狀態(tài)碼 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實現(xiàn)(成功會返回200,如果頁面找不到會返回404)
function GetHttpStatusCode($url){ $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,$url);//獲取內(nèi)容url curl_setopt($curl,CURLOPT_HEADER,1);//獲取http頭信息 curl_setopt($curl,CURLOPT_NOBODY,1);//不返回html的body信息 curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//返回數(shù)據(jù)流,不直接輸出 curl_setopt($curl,CURLOPT_TIMEOUT,30); //超時時長,單位秒 curl_exec($curl); $rtn= curl_getinfo($curl,CURLINFO_HTTP_CODE); curl_close($curl); return $rtn; } $url = "http://www.baidu.com"; GetHttpStatusCode($url);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 統(tǒng)一數(shù)據(jù)返回格式的解決方案
統(tǒng)?的數(shù)據(jù)返回格式使? @ControllerAdvice 和 ResponseBodyAdvice 的?式實現(xiàn),下面給大家分享Spring Boot 統(tǒng)一數(shù)據(jù)返回格式的解決方案,感興趣的朋友一起看看吧2024-03-03mybatis-plus之如何實現(xiàn)in嵌套sql
這篇文章主要介紹了mybatis-plus之如何實現(xiàn)in嵌套sql問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03SpringSecurity集成第三方登錄過程詳解(最新推薦)
在ThirdAuthenticationFilter 類的attemptAuthentication()方法中,我們通過authType類型,然后創(chuàng)建對應(yīng)的Authentication實現(xiàn)來實現(xiàn)不同方式的登錄,下面給大家分享SpringSecurity集成第三方登錄過程,感興趣的朋友一起看看吧2024-05-05使用Mybatis對數(shù)據(jù)庫進行單表操作的實現(xiàn)示例
這篇文章主要介紹了使用Mybatis對數(shù)據(jù)庫進行單表操作的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08java?SpringBoot?分布式事務(wù)的解決方案(JTA+Atomic+多數(shù)據(jù)源)
這篇文章主要介紹了java?SpringBoot?分布式事務(wù)的解決方案(JTA+Atomic+多數(shù)據(jù)源),文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-08-08