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 Implement LinkedBlockingQueue API
Spring MVC Async vs Spring WebFlux
Converting Strings to Enums in Java
Java Program to Implement Adjacency List
Check If Two Lists are Equal in Java
Guide to BufferedReader
Returning Custom Status Codes from Spring Controllers
Spring RestTemplate Request/Response Logging
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Java Program to Perform Cryptography Using Transposition Technique
Java Program to Implement Splay Tree
Simple Single Sign-On with Spring Security OAuth2
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Consuming RESTful Web Services
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Spring REST API + OAuth2 + Angular
Java Program to Perform Partial Key Search in a K-D Tree
Java Program to Implement ArrayBlockingQueue API
Java Program to Implement Stack using Two Queues
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Calling Stored Procedures from Spring Data JPA Repositories
Introduction to Spring Data JDBC
How To Serialize and Deserialize Enums with Jackson
Guide to the Fork/Join Framework in Java
A Guide to ConcurrentMap
Guide to java.util.concurrent.Locks
A Guide to LinkedHashMap in Java
Introduction to Java Serialization
Hướng dẫn sử dụng lớp Console trong java
Converting Iterator to List
Java Program to Implement Hamiltonian Cycle Algorithm
Removing Elements from Java Collections