Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

20
LINES

< > BotCompany Repo | #1001114 // checkIfHttpServerIsOnline

JavaX fragment (include)

1  
  // null = online, Throwable = offline
2  
  public static Throwable checkIfHttpServerIsOnline(String serverName) {
3  
    try {
4  
      String url = serverName.startsWith("http") ? serverName : "http://" + serverName + "/";
5  
      //HttpURLConnection.setFollowRedirects(false);
6  
      // note : you may also need
7  
      //        HttpURLConnection.setInstanceFollowRedirects(false)
8  
      HttpURLConnection con =
9  
         (HttpURLConnection) new URL(url).openConnection();
10  
      con.setRequestMethod("HEAD");
11  
      int responseCode = con.getResponseCode();
12  
      //print("Response code for " + url + ": " + responseCode);
13  
      // If we came this far, assume server is online.
14  
      return null;
15  
      //return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
16  
    }
17  
    catch (Exception e) {
18  
       return e;
19  
    }
20  
  }  

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001114
Snippet name: checkIfHttpServerIsOnline
Eternal ID of this version: #1001114/1
Text MD5: d4fd9e46fdbd0440578dedf0f9d31c07
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-22 18:04:28
Source code size: 833 bytes / 20 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 683 / 904
Referenced in: [show references]