Created
March 9, 2014 09:26
-
-
Save fidanov/9445127 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.package.name; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class Server { | |
public static class Result { | |
public int code; | |
public InputStream stream; | |
} | |
public static Result get(String url) { | |
try { | |
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection(); | |
connection.setDoInput(true); | |
connection.connect(); | |
Result result = new Result(); | |
result.code = connection.getResponseCode(); | |
if (result.code == 200) | |
result.stream = connection.getInputStream(); | |
else | |
result.stream = connection.getErrorStream(); | |
return result; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public static boolean ping() { | |
Result result = get("http://www.google.com"); | |
return result != null && result.code == 200; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment