Skip to content

Instantly share code, notes, and snippets.

@fidanov
Created March 9, 2014 09:26
Show Gist options
  • Save fidanov/9445127 to your computer and use it in GitHub Desktop.
Save fidanov/9445127 to your computer and use it in GitHub Desktop.
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