Example usage for java.net HttpURLConnection getLastModified

List of usage examples for java.net HttpURLConnection getLastModified

Introduction

In this page you can find the example usage for java.net HttpURLConnection getLastModified.

Prototype

public long getLastModified() 

Source Link

Document

Returns the value of the last-modified header field.

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

    long date = httpCon.getLastModified();
    if (date == 0)
        System.out.println("No last-modified information.");
    else/*from   w  ww . j  a v a 2 s .c  o m*/
        System.out.println("Last-Modified: " + new Date(date));

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com");
    HttpURLConnection http = (HttpURLConnection) u.openConnection();
    http.setRequestMethod("HEAD");
    System.out.println(u + "was last modified at " + new Date(http.getLastModified()));
}

From source file:Main.java

private static boolean confirmDownload(Context context, String stringUrl) {
    try {/* ww w.j a  v a  2s .c o  m*/
        URL url = new URL(stringUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        long candidateDate = urlConnection.getLastModified();
        final SharedPreferences prefsManager = PreferenceManager
                .getDefaultSharedPreferences(context.getApplicationContext());

        final long savedDate = prefsManager.getLong(stringUrl, 0);

        urlConnection.disconnect();
        if (candidateDate <= savedDate) {
            return false;
        }
        timeToConfirm = candidateDate;

        return true;

    } catch (Throwable localThrowable) {

        localThrowable.printStackTrace();
        return false;

    }
}

From source file:HttpUtil.java

public static long getURLLastModified(String url) {
    URL Url;//from ww w  .  ja va  2 s  . co m
    HttpURLConnection httpconn = null;
    long modtime;
    try {
        Url = new URL(url);
        httpconn = (HttpURLConnection) Url.openConnection();
        modtime = httpconn.getLastModified();
    } catch (Exception e) {
        modtime = -1;
    }
    if (httpconn != null)
        httpconn.disconnect();
    System.out.println("URL:" + url + " LastModified:" + new Date(modtime).toString());
    return modtime;
}

From source file:HttpUtil.java

public static boolean hasURLContentChanged(String url, long fromtime) {
    URL Url;//from   w  ww.ja v  a2  s.  c o  m
    HttpURLConnection httpconn = null;
    long modtime;
    boolean contentChanged = false;
    try {
        Url = new URL(url);
        httpconn = (HttpURLConnection) Url.openConnection();
        modtime = httpconn.getLastModified();
        if ((modtime > fromtime) || (modtime <= 0)) {
            System.out.println("*** Old Link time:" + new Date(fromtime).toString() + " New link time:"
                    + new Date(modtime).toString());
            contentChanged = true;
        }
    } catch (Exception e) {
        contentChanged = true; // assume content has changed
    }
    if (httpconn != null)
        httpconn.disconnect();
    return contentChanged;
}

From source file:gr.scify.newsum.Utils.java

/**
 * Returns the last modified HTTP property of a given URL.
 * @param url The url to examine//w  w w  .j av  a  2s. co  m
 * @return A long number indicating the modified header field
 * of the URL. 
 * @throws IOException If the connection to the URL fails.
 */
public static long lastModified(URL url) throws IOException {
    HttpURLConnection.setFollowRedirects(true);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    long date = con.getLastModified();

    return date;
}

From source file:org.owasp.dependencycheck.utils.Downloader.java

/**
 * Makes an HTTP Head request to retrieve the last modified date of the given URL. If the file:// protocol is
 * specified, then the lastTimestamp of the file is returned.
 *
 * @param url the URL to retrieve the timestamp from
 * @return an epoch timestamp/*  ww  w .j  ava  2s . c o  m*/
 * @throws DownloadFailedException is thrown if an exception occurs making the HTTP request
 */
public static long getLastModified(URL url) throws DownloadFailedException {
    long timestamp = 0;
    //TODO add the FTP protocol?
    if ("file".equalsIgnoreCase(url.getProtocol())) {
        File lastModifiedFile;
        try {
            lastModifiedFile = new File(url.toURI());
        } catch (URISyntaxException ex) {
            final String msg = String.format(
                    "Unable to locate '%s'; is the cve.url-2.0.modified property set correctly?",
                    url.toString());
            throw new DownloadFailedException(msg);
        }
        timestamp = lastModifiedFile.lastModified();
    } else {
        HttpURLConnection conn = null;
        try {
            conn = URLConnectionFactory.createHttpURLConnection(url);
            conn.setRequestMethod("HEAD");
            conn.connect();
            timestamp = conn.getLastModified();
        } catch (URLConnectionFailureException ex) {
            throw new DownloadFailedException("Error creating URL Connection for HTTP HEAD request.", ex);
        } catch (IOException ex) {
            throw new DownloadFailedException("Error making HTTP HEAD request.", ex);
        } finally {
            if (conn != null) {
                try {
                    conn.disconnect();
                } finally {
                    conn = null;
                }
            }
        }
    }
    return timestamp;
}

From source file:org.javaweb.utils.HttpRequestUtils.java

/**
 * HTTP????HttpResponse// w  ww .  j  ava 2 s  . c  o m
 *
 * @param httpURLConnection
 * @param response
 * @throws IOException
 */
public static void setResponse(HttpURLConnection httpURLConnection, HttpResponse response) throws IOException {
    response.setStatusCode(httpURLConnection.getResponseCode());
    response.setStatusMessage(httpURLConnection.getResponseMessage());
    response.setContentType(httpURLConnection.getContentType());
    response.setHeader(new CaseInsensitiveMap(httpURLConnection.getHeaderFields()));
    response.setLastModified(httpURLConnection.getLastModified());

    // ?Cookie
    setCookies(response);
}

From source file:biz.gabrys.lesscss.extended.compiler.source.HttpSource.java

/**
 * {@inheritDoc} Before the first {@link #getContent()} call returns current source modification time. After the
 * first {@link #getContent()} call always returns the source modification time read while downloading the source
 * contents.//from  w  w w  .ja v  a 2 s.  c  o m
 * @since 1.0
 */
public Date getLastModificationDate() {
    if (lastModificationDate != null) {
        return (Date) lastModificationDate.clone();
    }

    final HttpURLConnection connection = makeConnection(false);
    final Date date = getModificationDate(connection.getLastModified());
    connection.disconnect();
    return date;
}

From source file:org.jpublish.repository.web.WebRepository.java

/** Get the last modified time in milliseconds for the given path.
        //w w w  .ja v  a2s  .c  om
@param path The content path
@return The last modified time in milliseconds
@throws Exception Any exception
*/

public long getLastModified(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    return conn.getLastModified();
}