Tips for dealing with HTTP-related problems

1. Check if a file was modified on the server

URL u =null;

  long timestamp = 0;

  try {
     u = new URL(getDocumentBase(), "test.gif");
     URLConnection uc = u.openConnection();
     uc.setUseCaches(false);
     /*
     ** use timestamp has a reference, re-open an URLConnection
     ** to the same file to check if the timestamp is different
     ** with the getLastModified() method.
     */
     timestamp = uc.getLastModified();
  } 
  catch (Exception e) {
     e.printStackTrace();
  }
}

2. Check if a page exists

import java.net.*;
import java.io.*;

public class URLUtils {

  public static void main(String s[]) {
    System.out.println(URLUtils.exists("https://www.maixuanviet.com/how-to-install-and-secure-phpmyadmin-on-ubuntu.vietmx"));
    System.out.println(URLUtils.exists("https://www.maixuanviet.com/not-exists.vietmx"));
    /*
      output :
        true
        false
    */    
  }

  public static boolean exists(String URLName){
    try {
      HttpURLConnection.setFollowRedirects(false);
      // note : you may also need
      //        HttpURLConnection.setInstanceFollowRedirects(false)
      HttpURLConnection con =
         (HttpURLConnection) new URL(URLName).openConnection();
      con.setRequestMethod("HEAD");
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e) {
       e.printStackTrace();
       return false;
    }
  }
}

The following is doing the same thing but this time we identify ourselves to a proxy.

import java.net.*;
import java.io.*;
import java.util.Properties;

public class URLUtils {

  public static void main(String s[]) {
    System.out.println(exists("http://www.maixuanviet.com"));
    System.out.println(exists("http://www.luyenthithukhoa.vn"));
  }


  public static boolean exists(String URLName){
    try {
      Properties systemSettings = System.getProperties();
      systemSettings.put("proxySet", "true");
      systemSettings.put("http.proxyHost","proxy.mycompany.local") ;
      systemSettings.put("http.proxyPort", "80") ;

      URL u = new URL(URLName);
      HttpURLConnection con = (HttpURLConnection) u.openConnection();
      //
      // it's not the greatest idea to use a sun.misc.* class
      // Sun strongly advises not to use them since they can 
      // change or go away in a future release so beware.
      //
      sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
      String encodedUserPwd =
         encoder.encode("domain\\username:password".getBytes());
      con.setRequestProperty
         ("Proxy-Authorization", "Basic " + encodedUserPwd);
      con.setRequestMethod("HEAD");
      System.out.println
         (con.getResponseCode() + " : " + con.getResponseMessage());
      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
    }
    catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
}

Done! Happy Coding!

Related posts:

Java Program to Perform Encoding of a Message Using Matrix Multiplication
Implementing a Runnable vs Extending a Thread
Java Program to Implement PrinterStateReasons API
List Interface trong Java
Convert Character Array to String in Java
How to Read a File in Java
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Intro to Spring Boot Starters
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Java – Reader to String
Java String to InputStream
Một số tính năng mới về xử lý ngoại lệ trong Java 7
A Guide to EnumMap
Spring REST API + OAuth2 + Angular
Introduction to Spring Data JPA
Java Program to Implement Brent Cycle Algorithm
Hướng dẫn Java Design Pattern – Interpreter
JPA/Hibernate Persistence Context
Java Program to Implement Randomized Binary Search Tree
Removing all duplicates from a List in Java
@DynamicUpdate with Spring Data JPA
Java Program to Implement the One Time Pad Algorithm
How to Read a Large File Efficiently with Java
Java Program to Find Minimum Element in an Array using Linear Search
Consumer trong Java 8
Java Program to Implement Efficient O(log n) Fibonacci generator
A Guide to Queries in Spring Data MongoDB
Java Program to Describe the Representation of Graph using Incidence Matrix
Java Program to Check whether Directed Graph is Connected using DFS
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Giới thiệu Google Guice – Dependency injection (DI) framework
Java – Reader to InputStream