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:
Tránh lỗi NullPointerException trong Java như thế nào?
Spring Boot - Servlet Filter
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Spring Cloud Bus
Java Convenience Factory Methods for Collections
Java Timer
Giới thiệu Google Guice – Dependency injection (DI) framework
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Implement Queue
Java Program to Represent Graph Using Linked List
Java Program to Check if a Matrix is Invertible
Java Program to Implement Horner Algorithm
How to Change the Default Port in Spring Boot
Java Program to Search for an Element in a Binary Search Tree
Using Java Assertions
Introduction to Spring Boot CLI
Java Program to Implement Dijkstra’s Algorithm using Priority Queue
CharSequence vs. String in Java
Mapping a Dynamic JSON Object with Jackson
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Implement Efficient O(log n) Fibonacci generator
Java Program to Implement Floyd-Warshall Algorithm
Apache Camel with Spring Boot
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Functional Interfaces in Java 8
Spring Boot - Rest Template
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Java Program to Implement the Monoalphabetic Cypher
Comparing Dates in Java
Spring RestTemplate Error Handling
Model, ModelMap, and ModelAndView in Spring MVC