[轉貼]台鐵訂票程式 by java

轉貼自
http://www.javaworld.com.tw/jute/post/print?bid=35&id=33920

1.[網路]台鐵自動訂票程式 Copy to clipboard
Posted by: Yoshi
Posted on: 2004-02-21 07:36

老王賣瓜一下…
這個程式在過年節慶時是很有用的喔…可以不停的為你檢查有沒有人退票
今年過年我們教授就這樣訂到二張去高雄的火車票XD

Share your vote!


Do you like this post?
  • Fascinated
  • Happy
  • Sad
  • Angry
  • Bored
  • Afraid

4 thoughts on “[轉貼]台鐵訂票程式 by java”

  1. 29.Re:[網路]台鐵自動訂票程式 [Re: Yoshi] Copy to clipboard
    Posted by: zanta
    Posted on: 2004-05-10 11:34

    首先,感謝 Yoshi 跟 titaeric 兩位的程式碼,讓小弟有一個很好的練習程式。

    小弟根據 titaeric 修改過的程式再加以修改 (好像繞口令..),改過的幾個小地方條列如下:

    1) 在設定的文字檔中加入 key 值 (也就是文字檔中是以 key=value 的格式在記錄資料),同時將網址以及等待時間這兩項參數加進文字檔中。

    2) 加上時間以作為訂票依據 (因台鐵網路訂票有開放時間限制..),這邊用到 SimpleDateFormat 這個 class。

    3) 若未成平q到票,則將 Server 回傳的訊息取出並秀在畫面上。這邊用到 Regular Expression 將 <...> 以及 標籤去除,留下純文字部份。

    原本是改成利用 ResouceBundle 讀取 .properties 檔的方式載入訂票資訊
    但測試時發現一個奇怪的問題,也就是訂票時的參數順序改變的話 (比如,第一個參數不是 personId),台鐵的網站就會拒絕我的訂票 Request..
    在想出解法前,還是暫時以讀取純文字檔來執行好了…

    附圖是執行時的樣子,程式碼跟文字設定檔請見下面;執行時第一個參數為設定檔的檔名。
    小弟初學 java,若改的不好還請見諒

    OrderTicket.java

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Date;
    import java.text.SimpleDateFormat;

    public class OrderTicket
    {
    private static StringBuffer url = new StringBuffer(“”);
    private static int wait;

    public OrderTicket()
    {
    super();
    }

    public OrderTicket(String file) throws IOException
    {
    /* 參數意義
    “personId”, //身分證字號
    “fromStation”, //起站代碼
    “toStation”, //到站代碼
    “getinDate”, //乘車日期
    “orderQty”, //訂票張數
    “trainNo”, //車次代碼
    “returnTicket” //是否訂來回票
    */

    BufferedReader inputFile = new BufferedReader(new FileReader(file));
    String line = null;

    while ( (line = inputFile.readLine()) != null )
    {
    this.parse(line.trim());
    }

    inputFile.close();
    }

    public static void main(String[] args) throws Exception
    {
    if (args.length != 1)
    {
    System.err.println(“參數不正確!Expected : String fileName”);
    //System.in.read(); //Pause
    System.exit(0);
    }
    /*
    try
    {
    new OrderTicket(args[0]).order();
    }
    catch(ArrayIndexOutOfBoundsException ex)
    {
    System.out.println(“設定檔格式錯誤,請確認是否符合 key=value 之形式。”);
    }
    catch (NumberFormatException ex)
    {
    System.out.println(“等待時間不為數字格式,請檢查設定檔中 “wait=” 參數。”);
    }
    catch (IOException ex)
    {
    System.out.println(“連線失敗,請檢查您的網路連線。”);
    }
    */
    }

    private void parse(String input) throws ArrayIndexOutOfBoundsException, NumberFormatException
    {
    String[] tmp = input.split(“=”);
    String key = tmp[0].trim();
    String value = tmp[1].trim();

    if (key.equals(“url”))
    {
    url.append(value);
    }
    else if (key.equals(“wait”))
    {
    wait = Integer.parseInt(value);
    }
    else
    {
    if (url.toString().endsWith(“&”) || url.toString().endsWith(“?”))
    {
    url.append(key + “=” + value);
    }
    else
    {
    url.append(“&” + key + “=” + value);
    }
    }
    }// End of parse()

    private void order() throws IOException
    {
    System.out.println(url);
    URL website = new URL(url.toString());
    String dateFormat = “yyyy’/’MM’/’dd, HH:mm:ss”;
    int count = 1;

    while (true)
    {
    System.out.print(“n開始第 【” + (count++) + “】 次嚐試訂票 -“);
    System.out.println(“【” + new SimpleDateFormat(dateFormat).format(new Date()) + “】—————“);

    URLConnection conn = website.openConnection();
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    StringBuffer content = new StringBuffer(“”);
    String line = null;

    while ((line = in.readLine()) != null)
    {
    content.append(line);
    }

    in.close();
    this.showResult(content.toString());
    }
    }// End of order()

    private void showResult(String content) throws IOException
    {
    if (content.indexOf(“您的車票已訂到”) != -1)
    {
    BufferedWriter bw = new BufferedWriter(new FileWriter(“result.html”));

    bw.write(content);
    bw.close();

    System.out.println(“nt★ 票己成平q到,請讀取 result.html 檔。”);
    System.exit(0);
    }
    else
    {
    String tagPattern = “(<{1}[^>]{1,}>{1})|(&nbsp)”; //去除 <..> 與 “&nbsp”,因顯示問題,請將全形符號&改成半形
    String failMsg = content.replaceAll(tagPattern, “”);

    System.out.println(“nt執行結果:票尚未訂到,” + (wait/1000) + ” 秒鐘之後將會再次嚐試…”);
    System.out.println(“t回傳訊息:” + failMsg.trim());

    try
    {
    Thread.sleep(wait); //sleep some time, and try it again later.
    }
    catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    }
    }// End of showResult()
    }

    設定文字檔 (範例)

    url = http://railway.hinet.net/trw/ctno11.jsp?
    wait = 60000
    personId = A123456789
    fromStation = 004
    toStation = 098
    getinDate = 040523
    orderQty = 1
    trainNo = 1059
    returnTicket = 0

  2. (1)
    請問文字設定檔的檔名為何? 我試過 “設定文字檔.txt” ,”url.txt” , “personId.txt” 都不對.
    都會出現 “參數不正確!Expected : String fileName”.
    (2)
    請問, 可以直接寄給我 “OrderTicket.class” 和 文字設定檔嗎?
    (3)
    我的目的是要讓我媽媽可以使用的, 並不是有惡意的

    謝謝!!

Leave a Reply to worren Cancel reply

Your email address will not be published. Required fields are marked *


Please help to input verification code