Good morning.
I am fairly new to Java, so please be patient.
I am currently working on a small Java class to recover the header of a URL to determine the last modification date. I am using a previously developed Java class that does it for HTTP, but want to extend it to HTTPS. I know that in general certificates should be imported into a keystore file so that secure sockets can refer to them. However, I would like to be able to load the certificate from a file dynamically.
The original HTTP code is the following (using JDK 1.5)
|
Code:
|
package com.xxx.utils.http;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class HttpFile {
private String url = null;
private HttpURLConnection urlConn = null;
public HttpFile(String url) {
this.url = url;
if (this.url != null) {
try {
URL u = new URL(url);
URLConnection conn = u.openConnection();
this.urlConn = (HttpURLConnection)conn;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void Finalize() {
if (this.urlConn != null) {
this.urlConn.disconnect();
}
this.urlConn = null;
}
public boolean exists() {
try {
if (this.urlConn != null) {
return (this.urlConn.getResponseCode() == HttpURLConnection.HTTP_OK);
} else {
return false;
}
}
catch (Exception ex) {
return false;
}
}
public long lastModified() {
if (this.exists()) {
return this.urlConn.getHeaderFieldDate("Last-Modified", 0);
} else {
return 0L;
}
}
public static boolean exists(String url) {
HttpFile hf = new HttpFile(url);
boolean exists = hf.exists();
hf.Finalize();
return exists;
}
public static long lastModified(String url) {
HttpFile hf = new HttpFile(url);
long lastModified = hf.lastModified();
hf.Finalize();
return lastModified;
}
} |
If you have any ideas, I would certainly appreciate it.