Java Utililty Methods URL Value Check

List of utility methods to do URL Value Check

Description

The list of methods to do URL Value Check are organized into topic(s).

Method

StringinvokePostUrl(String url, String encodedQueryString)
invoke Post Url
String agent = "Mozilla/4.0";
HttpURLConnection conn = null;
DataOutputStream output = null;
DataInputStream in = null;
BufferedReader is = null;
try {
    URL postURL = new URL(url);
    conn = (HttpURLConnection) postURL.openConnection();
...
StringinvokeUrl(String url)
This is a generic method to invoke the REST API.
if (url == null) {
    throw new IllegalArgumentException("Null parameter passed in");
URL urlObj;
InputStream inStream = null;
String response = null;
HttpURLConnection connection = null;
try {
...
booleanis200(final String url)
is
HttpURLConnection con = null;
try {
    con = (HttpURLConnection) new URL(url).openConnection();
    return (con.getResponseCode() == 200);
} catch (Exception e) {
    return false;
} finally {
    if (con != null) {
...
booleanis404(URL url)
is
URLConnection connection;
InputStream urlIn = null;
try {
    connection = url.openConnection();
    if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        return httpConnection.getResponseCode() >= 400;
    urlIn = connection.getInputStream();
    urlIn.read();
} catch (IOException ex) {
    return true;
} finally {
    try {
        if (urlIn != null) {
            urlIn.close();
    } catch (IOException e) {
return false;
booleanisAddressReachable(String url, int timeout)
is Address Reachable
try {
    URL test = new URL(url);
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection urlConnect = (HttpURLConnection) test.openConnection();
    System.setProperty("http.agent", "");
    urlConnect.setRequestProperty("User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30");
    urlConnect.setRequestMethod("HEAD");
...
booleanisAValidURL(String url)
is A Valid URL
URL u = null;
HttpURLConnection huc = null;
try {
    u = new URL(url);
    huc = (HttpURLConnection) u.openConnection();
    huc.setRequestMethod("HEAD");
    huc.connect();
    return (huc.getResponseCode() == HttpURLConnection.HTTP_OK);
...
booleanisCompressed(HttpURLConnection connection)
is Compressed
for (Map.Entry<String, List<String>> entry : connection.getHeaderFields().entrySet()) {
    if ("Content-Encoding".equalsIgnoreCase(entry.getKey())) {
        return "gzip".equalsIgnoreCase(entry.getValue().get(0));
return false;
booleanisEMailAddress(URL url)
is E Mail Address
boolean result = true;
String urlString = url.toString();
if (url.getHost() != null && !url.getHost().trim().equals("")) {
    result = false;
if (url.getPort() != -1) {
    result = false;
int pos = urlString.indexOf('@');
if (pos == -1) {
    result = false;
} else {
    if (urlString.indexOf('@', pos + 1) != -1) {
        return false;
if (pos > (urlString.length() - 6)) {
    result = false;
return result;
booleanisFile(final URL url)
Indicates whether or not the given url is a file.
return url != null && new File(url.getFile()).isFile();
booleanisFile(final URL url)
is File
return url != null && (url.getProtocol().equals(PROTOCOL_FILE) || url.getProtocol().equals(JBOSS_FILE));