HttpUtil.java Source code

Java tutorial

Introduction

Here is the source code for HttpUtil.java

Source

/**********************************************************************************
    
Feedzeo! 
A free and open source RSS/Atom/RDF feed aggregator
    
Copyright (C) 2005-2006  Anand Rao (anandrao@users.sourceforge.net)
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    
************************************************************************************/

/*
 * HttpUtil.java
 *
 * Created on November 12, 2005, 9:12 PM
 */

import java.util.*;
import java.net.*;

/**
 *
 * @author  Anand Rao
 */
public class HttpUtil {

    public static boolean hasURLContentChanged(String url, long fromtime) {
        URL Url;
        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;
    }

    public static long getURLLastModified(String url) {
        URL Url;
        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;
    }

    public static String getDomainName(String url) {
        URL u;
        try {
            u = new URL(url);
        } catch (Exception e) {
            return "";
        }
        return u.getHost();
    }

}