How to quickly check if URL server is available

后端 未结 8 2041
执念已碎
执念已碎 2020-12-09 16:30

I have a URL in the form

http://www.mywebsite.com/util/conv?a=1&from=%s&to=%s

And want to check if it is available.

The lin

8条回答
  •  轮回少年
    2020-12-09 16:47

    public static boolean exists(String URLName) {
    
            try {
                HttpURLConnection.setFollowRedirects(false);
                // note : you may also need
                // HttpURLConnection.setInstanceFollowRedirects(false)
                HttpURLConnection con = (HttpURLConnection) new URL(URLName)
                .openConnection();
                con.setRequestMethod("HEAD");
                return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    

提交回复
热议问题