嗨,我知道在Firefox上有一个网站检查器扩展,将通过Firefox通知网站是否已更新。如何检查网站是否已更新并发送电子邮件?
是否有任何代码片段用于执行相同的功能?我想通过电子邮件通知进行网站更新。
嗨,我知道在Firefox上有一个网站检查器扩展,将通过Firefox通知网站是否已更新。如何检查网站是否已更新并发送电子邮件?
是否有任何代码片段用于执行相同的功能?我想通过电子邮件通知进行网站更新。
通过在您的URL对象上调用openConnection()
来使用HttpUrlConnection
。
getResponseCode()
会在您从连接中读取后给您HTTP响应。
例如
URL u = new URL ("http://www.example.com/");
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod ("GET");
huc.connect() ;
OutputStream os = huc.getOutputStream () ;
int code = huc.getResponseCode () ;
(未测试!)
这做工作持续了页面内容的最后SHA2哈希和对每5秒一个持久化比较当前的哈希值。顺便说一下,exmaple依赖apache编解码器库进行sha2操作。
import org.apache.commons.codec.digest.*;
import java.io.*;
import java.net.*;
import java.util.*;
/**
* User: jhe
*/
public class UrlUpdatedChecker {
static Map<String, String> checkSumDB = new HashMap<String, String>();
public static void main(String[] args) throws IOException, InterruptedException {
while (true) {
String url = "http://www.stackoverflow.com";
// query last checksum from map
String lastChecksum = checkSumDB.get(url);
// get current checksum using static utility method
String currentChecksum = getChecksumForURL(url);
if (currentChecksum.equals(lastChecksum)) {
System.out.println("it haven't been updated");
} else {
// persist this checksum to map
checkSumDB.put(url, currentChecksum);
System.out.println("something in the content have changed...");
// send email you can check: http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274
}
Thread.sleep(5000);
}
}
private static String getChecksumForURL(String spec) throws IOException {
URL u = new URL(spec);
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.setDoOutput(true);
huc.connect();
return DigestUtils.sha256Hex(huc.getInputStream());
}
}
你是什么意思,检查网站是否已更新?请提供更多细节。 –
如果有RSS提要,您可以使用... – Jasper