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:
Extract links from an HTML page
How to Get All Spring-Managed Beans?
Converting Strings to Enums in Java
Using the Not Operator in If Conditions in Java
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java 8 StringJoiner
Java Program to Find Strongly Connected Components in Graphs
Spring Data Reactive Repositories with MongoDB
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java Program to Implement Leftist Heap
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
DynamoDB in a Spring Boot Application Using Spring Data
Java Program to Implement Shoelace Algorithm
Spring Security 5 – OAuth2 Login
Spring MVC and the @ModelAttribute Annotation
Spring Boot - Bootstrapping
Interface trong Java 8 – Default method và Static method
Spring Security and OpenID Connect
Sort a HashMap in Java
Instance Profile Credentials using Spring Cloud
Java Program to Implement IdentityHashMap API
An Intro to Spring Cloud Contract
Cơ chế Upcasting và Downcasting trong java
Java String Conversions
Java Program to Implement ArrayList API
Java Program to Check if it is a Sparse Matrix
Spring 5 WebClient
Ép kiểu trong Java (Type casting)
Spring MVC Setup with Kotlin