Java 如何检查网站是否已更新并发送电子邮件?
您好,我知道在Firefox上有一个网站检查器扩展,它将通过Firefox通知网站是否已更新Java 如何检查网站是否已更新并发送电子邮件?,java,Java,您好,我知道在Firefox上有一个网站检查器扩展,它将通过Firefox通知网站是否已更新 是否有任何代码片段用于执行相同的功能?我想用电子邮件通知网站更新。通过调用URL对象上的openConnection()使用HttpUrlConnection getResponseCode()将在读取连接后为您提供HTTP响应 e、 g URL u = new URL ( "http://www.example.com/" ); HttpURLConnection huc = ( Ht
是否有任何代码片段用于执行相同的功能?我想用电子邮件通知网站更新。通过调用URL对象上的
openConnection()
使用HttpUrlConnection
getResponseCode()
将在读取连接后为您提供HTTP响应
e、 g
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());
}
}
import org.apache.commons.codec.digest.*;
导入java.io.*;
导入java.net。*;
导入java.util.*;
/**
*用户:jhe
*/
公共类UrlUpdatedChecker{
静态映射checkSumDB=newhashmap();
公共静态void main(字符串[]args)引发IOException、InterruptedException{
while(true){
字符串url=”http://www.stackoverflow.com";
//从映射查询最后一个校验和
字符串lastChecksum=checkSumDB.get(url);
//使用静态实用程序方法获取当前校验和
字符串currentChecksum=getChecksumForURL(url);
if(当前校验和等于(上次校验和)){
System.out.println(“尚未更新”);
}否则{
//将此校验和持久化为映射
checkSumDB.put(url、currentChecksum);
System.out.println(“内容中的某些内容已更改…”);
//发送电子邮件,您可以查看:http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274
}
睡眠(5000);
}
}
私有静态字符串getChecksumForURL(字符串规范)引发IOException{
URL u=新的URL(规范);
HttpURLConnection huc=(HttpURLConnection)u.openConnection();
huc.setRequestMethod(“GET”);
huc.setDoOutput(真);
huc.connect();
返回DigestUtils.sha256Hex(huc.getInputStream());
}
}
检查网站是否已更新是什么意思?请提供更多详细信息。如果有RSS源,您可以使用它。。。