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 – InputStream to Reader
Flattening Nested Collections in Java
Java – Reader to InputStream
Redirect to Different Pages after Login with Spring Security
Lớp Arrarys trong Java (Arrays Utility Class)
Spring Boot - Google OAuth2 Sign-In
Java Program to Implement Insertion Sort
Java Program to Encode a Message Using Playfair Cipher
Get the workstation name or IP
Sorting in Java
Retrieve User Information in Spring Security
Jackson vs Gson
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Implement Attribute API
Spring Boot - Enabling Swagger2
Introduction to Spring MVC HandlerInterceptor
Giới thiệu Json Web Token (JWT)
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Concurrent Test Execution in Spring 5
Java List UnsupportedOperationException
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Implement Leftist Heap
Spring Security – security none, filters none, access permitAll
Error Handling for REST with Spring
So sánh ArrayList và LinkedList trong Java
Java Program for Douglas-Peucker Algorithm Implementation
Reactive WebSockets with Spring 5
Java Program to Implement Circular Singly Linked List
Java Program to Implement Extended Euclid Algorithm