一個將程式應用在生活上的好例子,利用標準 HTTP 協定來達成台鐵自動訂票的需求!!
記得以前也寫過類似的程式,去 CNN 網站狂投公投綁大選的反對票,呵呵!!
已經很久沒坐火車了,這個程式雖然沒有 GUI 介面,不過應該不難使用,推薦給大家使用!!
—————————————————————-
研究了一下台鐵訂票的html, 裡面用到一個JavaScript檔
不過是用*.js的方式與html分開, 有興趣的人可以另存網頁
再打開來研究裡面的語法…
不過就我個人使用而言, 直接指定車次去訂票比較符合我的需求
所以我修改了Yoshi兄的code,並且做了一些精簡,
另外,把訂票需要的資訊(身分證字號,起站代碼,到站代碼…)與程式分開
寫在文字檔中, 當需要訂不同的票時只要修改此文字檔, 不需重新compile
第一次post程式 請指教
1 |
import java.net.*;
import java.io.*;
public class OrderTicket {
private static String url="http://railway.hinet.net/trw/ctno11.jsp?";
public static void add(String attribute, String value) {
if(url.endsWith("&") || url.endsWith("?")) {
url+= attribute + "=" + value;
}
else {
url+= "&" + attribute + "=" + value;
}
}
public static void order() throws IOException {
URL website = new URL(url);
int count=1;
while(true) {
System.out.println("開始第" + count + "次嚐試訂票...");
count++;
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line=null;
String content="";
while( (line=in.readLine())!=null) {
content+=line;
System.out.print(".");
}
in.close();
if(content.indexOf("您的車票已訂到")!=-1) {
BufferedWriter bw = new BufferedWriter(new FileWriter("result.html"));
bw.write(content);
bw.close();
System.out.println("票己成功訂到,請讀取result.html檔。");
System.exit(0);
}
else {
System.out.println("票尚未訂到,一分鐘之後將會再次嚐試...");
try {
Thread.sleep(6000); //sleep 1 minute, and try it again later.
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}//end while
}
public static void main(String[] args) throws Exception{
if (args.length != 1)
{
// Print message, pause, then exit
System.err.println ("Invalid command parameters");
System.in.read();
System.exit(0);
}
BufferedReader filein = new BufferedReader(new FileReader(args[0] ));
//BufferedReader filein = new BufferedReader(new FileReader("data.txt"));
int i=0;
String[] value = new String[10];
String[] attribute = {"personId",//身分證字號
"fromStation",//_站代碼
"toStation",//到站代碼
"getinDate",//乘車日期
"orderQty",//訂票張數
"trainNo",//車次代碼
"returnTicket"//是否訂來回票
};
while ((value[i] = filein.readLine()) != null) {
add(attribute[i],value[i] );
i++;
}
System.out.println(url);
/*
try {
order();
}
catch(IOException e) {
System.out.println("連線失敗,請檢查你的網路連線。");
}
*/
}
}
|
編譯完 需要另外新增一個文字檔案儲存訂票資訊
執行時的第一個參數為此文字檔案檔名
這種立可拍防水相機大約兩百元左右,不過這個可是傳統底片相機,拍回來沖洗後還需拿底掃再掃一次,才能得到數位影像,而且這種相機大概也別想要求影像品質了,主要訴求就是能夠讓你在水裡拍攝而已。
不過科技日新月異,漸漸的數位相機也被設計成可以在水裡攝影。方式分成兩種:
1.潛水盒,顧名思義就是類似一個盒子套在數位相機外面,而潛水深度可達 30 米 ~ 40 米。由於潛水深度夠深,所以潛水盒的防水性絕佳,操作得宜的話,幾乎不可能進水。但是缺點就是,必須針對所使用的數位相機型號來購買,而且價位很貴,大約八九千元,幾乎都可以再買一台 DC 了。













