上海学网站建设,网站建网站建设,宜昌市工程造价信息网,威海公司注册这篇文章主要介绍了如何基于java或js获取URL返回状态码,文中通过示例代码介绍的非常详细#xff0c;对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参fgLAeaGAH考下描述#xff1a;使用java或者js访问某个网站#xff0c;返回状态码1.java实现// 用getResponse…这篇文章主要介绍了如何基于java或js获取URL返回状态码,文中通过示例代码介绍的非常详细对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参fgLAeaGAH考下描述使用java或者js访问某个网站返回状态码1.java实现// 用getResponseCode可以获取URL返回状态码String surl ;try {surl你的url;URL url new URL(suwww.cppcns.comrl);URLConnection rulConnection url.openConnection();HttpURLConnection httpUrlConnection (HttpURLConnection) rulConnection;httpUrlConnection.setConnectTimeout(300000);httpUrlConnection.setReadTimeout(300000);httpUrlConnection.connect();Swww.cppcns.comtring 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,codecode);}System.out.println(getCurDateTime()连接surl正常);}catch(Exception ex){System.out.println(ex.getMessage());}2.js实现(成功会返回200如果页面找不到会返回404)function GetHttpStatusCode($url){$curl curl_init();curl_setopt($curl,CURLOPT_URL,$url);//获取内容urlcurl_setopt($curl,CURLOPT_HEADER,1);//获取http头信息curl_setopt($curl,CURLOPT_NOBODY,1);//不返回hhttp://www.cppcns.comtml的body信息curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//返回数据流不直接输出curl_setopt($curl,CURLOPT_TIMEOUT,30); //超时时长单位秒curl_exec($curl);$rtn curl_getinfo($curl,CURLINFO_HTTP_CODE);cwww.cppcns.comurl_close($curl);return $rtn;}$url http://www.baidu.comGetHttpStatusCode($url);以上就是本文的全部内容希望对大家的学习有所帮助也希望大家多多支持我们。本文标题: 如何基于java或js获取URL返回状态码本文地址: http://www.cppcns.com/wangluo/javascript/289361.html