01: /**********************************************************************************
02:
03: Feedzeo!
04: A free and open source RSS/Atom/RDF feed aggregator
05:
06: Copyright (C) 2005-2006 Anand Rao (anandrao@users.sourceforge.net)
07:
08: This library is free software; you can redistribute it and/or
09: modify it under the terms of the GNU Lesser General Public
10: License as published by the Free Software Foundation; either
11: version 2.1 of the License, or (at your option) any later version.
12:
13: This library is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16: Lesser General Public License for more details.
17:
18: You should have received a copy of the GNU Lesser General Public
19: License along with this library; if not, write to the Free Software
20: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21:
22: ************************************************************************************/package util;
23:
24: import java.util.*;
25: import java.net.*;
26:
27: /**
28: *
29: * @author Anand Rao
30: */
31: public class HttpUtil {
32:
33: /** Creates a new instance of HttpUtil */
34: public HttpUtil() {
35: }
36:
37: public static boolean hasURLContentChanged(String url, long fromtime) {
38: URL Url;
39: HttpURLConnection httpconn = null;
40: long modtime;
41: boolean contentChanged = false;
42: try {
43: Url = new URL(url);
44: httpconn = (HttpURLConnection) Url.openConnection();
45: modtime = httpconn.getLastModified();
46: if ((modtime > fromtime) || (modtime <= 0)) {
47: System.out.println("*** Old Link time:"
48: + new Date(fromtime).toString()
49: + " New link time:"
50: + new Date(modtime).toString());
51: contentChanged = true;
52: }
53: } catch (Exception e) {
54: ExceptionUtil.reportException(e);
55: contentChanged = true; // assume content has changed
56: }
57: if (httpconn != null)
58: httpconn.disconnect();
59: return contentChanged;
60: }
61:
62: public static long getURLLastModified(String url) {
63: URL Url;
64: HttpURLConnection httpconn = null;
65: long modtime;
66: try {
67: Url = new URL(url);
68: httpconn = (HttpURLConnection) Url.openConnection();
69: modtime = httpconn.getLastModified();
70: } catch (Exception e) {
71: ExceptionUtil.reportException(e);
72: modtime = -1;
73: }
74: if (httpconn != null)
75: httpconn.disconnect();
76: System.out.println("URL:" + url + " LastModified:"
77: + new Date(modtime).toString());
78: return modtime;
79: }
80:
81: public static String getDomainName(String url) {
82: URL u;
83: try {
84: u = new URL(url);
85: } catch (Exception e) {
86: ExceptionUtil.reportException(e);
87: return "";
88: }
89: return u.getHost();
90: }
91:
92: }
|