How to quickly check if URL server is available

后端 未结 8 2034
执念已碎
执念已碎 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:41

    if (android.os.Build.VERSION.SDK_INT > 9) {
                                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                        .permitAll().build();
    
                                StrictMode.setThreadPolicy(policy);
                            }
                            try {
                                URL diachi = new URL("http://example.com");
                                HttpURLConnection huc = (HttpURLConnection) diachi.openConnection();
                                huc.setRequestMethod("HEAD");
                                int responseCode = huc.getResponseCode();
    
                                if (responseCode != 404) {
                                    //URL Exist
    
                                } else {
                                    //URL not Exist
                                }
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
    

提交回复
热议问题