java 判断远程服务器http是否存在

来源:互联网 发布:上师大网络教育 编辑:程序博客网 时间:2024/05/21 04:40
 URL url = new URL(urlStr);            HttpURLConnection conn = (HttpURLConnection)url.openConnection();                    //设置超时间为3秒          conn.setConnectTimeout(3*1000);          //prevent 403 error          //conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");          int state=conn.getResponseCode();1.可以通过判断state状态,state=200时是存在的。2.可以通过 int fileSize = conn.getContentLength();判断返回的内容大小。来大概判断。
ubuntu sudo -s可以变为root用户



这里注意测试,因为在实际使用的过程中测试了一个http://baidu.com/a.jpg,这样同样返回的是200的状态,原因未知。但内网测试的时候是可以正常使用的。

0 0