-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifModifiedSince.java
40 lines (33 loc) · 1.41 KB
/
ifModifiedSince.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 16. Write a program to set ifModifiedSince to 24 Hours prior to Now.
import java.io.*;
import java.net.*;
import java.util.*;
public class ifModifiedSince {
public static void main(String[] args) {
// Initialize a Date object with the current date and time
Date today = new Date();
long millisecondsPerDay = 24 * 60 * 60 * 1000;
// Define the URL you want to test
String[] urls = {"https://www.facebook.com"};
for (String urlString : urls) {
try {
URL u = new URL(urlString);
URLConnection uc = u.openConnection();
System.out.println("Original if modified since: " + new Date(uc.getIfModifiedSince()));
// Set If-Modified-Since to 24 hours ago
uc.setIfModifiedSince(today.getTime() - millisecondsPerDay);
System.out.println("Will retrieve file if it's modified since " + new Date(uc.getIfModifiedSince()));
try (InputStream in = new BufferedInputStream(uc.getInputStream())) {
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read()) != -1) {
System.out.print((char) c);
}
System.out.println();
}
} catch (IOException ex) {
System.err.println(ex);
}
}
}
}