01:import java.io.InputStreamReader ;
02:import java.io.BufferedReader;
03:import java.net.URL;
04:import java.net.URLConnection;
05:import java.util.Date;
06:import java.text.DateFormat;
07:
08:public class HttpTextRead2 {
09:    static String[] enc = { 
10:        "ISO-2022-JP",
11:        "Shift_JIS",
12:        "EUC-JP",
13:        "UTF-8",
14:        "UTF-16",
15:        "x0208", // JIS_X0208_1983
16:    };
17:
18:    public static void main(String[] args) {
19:        String name = "file:localfile.txt" ;                // 読むファイルのURL(デフォルト)
20:        if(args.length>0) name = args[0];                   // コマンドラインがあれば採用
21:        if(name.equals("/H")||name.equals("?")
22:                            ||name.equals("/?")) {
23:            for(int i=0; i<enc.length-1; i++) 
24:                System.out.print(enc[i] +",");              // エンコーディング例を表示
25:            System.out.println(enc[enc.length-1]);
26:            System.exit(0);
27:        }
28:
29:        String encoding = enc[0];                           // エンコーディングのデフォルト
30:        if(args.length>1) encoding = args[1];               // コマンドラインの指定
31:
32:        URLConnection con = null;                           // コネクションの準備
33:        try {
34:            URL url = new URL( name );                      // URL を生成
35:            con = url.openConnection();                     //
36:            con.setDoInput(true);                           //
37:            con.setDoOutput(false);                         //
38:            con.setUseCaches(false);                        //
39:            con.setRequestProperty("connection", "close");  //
40:            if(args.length>2) {                             // コマンドラインに日付指定あり
41:                DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT);
42:                try{
43:                    Date date = formatter.parse(args[2]);   // yy/mm/dd から Date に変換
44:                    con.setIfModifiedSince(date.getTime()); // この日付より新しい場合にのみアクセス
45:                } catch(java.text.ParseException e) {
46:                    System.out.println(e.toString());
47:                }
48:            }
49:            con.connect();                                  //
50:        } catch(java.net.MalformedURLException e) {
51:            System.out.println("URL不正");                  // new RRL() 失敗
52:            System.exit(0);
53:        } catch(java.io.IOException e) {
54:            System.out.println("コネクト失敗");             // openConnection か connect の失敗
55:            System.exit(0);
56:        }
57:
58:        System.out.println("Headers: ");
59:        for(int i=0; true ; i++) {                          // ヘッダがあるかぎり
60:            String value = con.getHeaderField(i);
61:            if(value==null) break;                          //
62:            String key = con.getHeaderFieldKey(i);
63:            System.out.println(" " + key + ": " + value);   // キーと値を表示
64:        }
65:        System.out.println("-----------");
66:
67:        int len = con.getContentLength();
68:        try {
69:            BufferedReader in =                                         // コネクションから
70:                new BufferedReader(                                     // バッファリーダを作成
71:                new InputStreamReader( con.getInputStream(), encoding ) );
72:            String line = in.readLine();                                // 先頭行
73:            while(line!=null) {                                         // 行があれば
74:                System.out.println( line );                             //   表示
75:                line = in.readLine();                                   // 次の行
76:            }
77:        } catch(java.io.IOException e) {
78:            System.out.println("読み込み失敗");
79:        }
80:    }