2011-07-30 5 views
3

こんにちは私は、Firefox上に、Webサイトが更新されたかどうかFirefoxに通知するWebサイトチェッカー拡張機能があることを知っています。ウェブサイトが更新されてメールを送信しているかどうかを確認するにはどうすればよいですか?

同じ機能を実行するためのコードスニペットはありますか?私は代わりにウェブサイトの更新のための電子メール通知を持っていたいと思います。

+0

ウェブサイトが更新されているかどうかを確認することはどういう意味ですか?詳細をご記入ください。 –

+0

RSSフィードがあればそれを使うことができます... – Jasper

答えて

0

HttpUrlConnectionをURLオブジェクトでopenConnection()と呼んでください。

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 () ; 

(テストしていません!)

2

これは、ジョブがページのコンテンツの最後のSHA2ハッシュを持続し、5秒ごとに存続しているものに対して、現在のハッシュを比較しません。ところで、exmapleはsha2操作のためのapacheコーデックライブラリに依存しています。

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()); 
    } 
} 
関連する問題