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 for Douglas-Peucker Algorithm Implementation
Apache Commons Collections Bag
Phương thức tham chiếu trong Java 8 – Method References
Guide to Java 8’s Collectors
Generating Random Dates in Java
How to Set TLS Version in Apache HttpClient
Spring Autowiring of Generic Types
Build a REST API with Spring and Java Config
New Features in Java 9
Java Program to Implement ArrayBlockingQueue API
Apache Commons Collections OrderedMap
Spring NoSuchBeanDefinitionException
Java Program to Implement Binomial Heap
Java Program to Check Cycle in a Graph using Topological Sort
Serialize Only Fields that meet a Custom Criteria with Jackson
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Convert Hex to ASCII in Java
Introduction to Using Thymeleaf in Spring
Hướng dẫn Java Design Pattern – MVC
Using the Not Operator in If Conditions in Java
Java Program to Implement CopyOnWriteArraySet API
Java Program to Represent Linear Equations in Matrix Form
Serialization và Deserialization trong java
A Guide to LinkedHashMap in Java
Java – Reader to InputStream
Convert String to Byte Array and Reverse in Java
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
A Quick Guide to Using Keycloak with Spring Boot
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Constructor Injection in Spring with Lombok
Spring – Injecting Collections
Java Program to Implement AA Tree