注意:
1.实现了从登陆到查询出票的所有过程
2.代码简单,但功能全在,具体的查询信息条件没有做,例子里面是明天的北京到上海的车票有余票的
3.代码仅供学习,对了解httpClient,http+ssl协议有些小小的帮助。
4.运行代码,需要修改四个变量myStore,codeImg,loginUserName,loginPassWord
先上代码:
/**
* @Title: GetCodeImg.java
* @Package com.renfy.test
* @Description: TODO
* @author renfy
* @date 2013-7-24 下午04:59:58
* @version V1.0
*/
package com.renfy.ssl.ticket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.KeyStore;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.GzipDecompressingEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
/**
* @ClassName GetCodeImg
* @Description TODO
* @author renfy
* @date 2013-7-24 下午04:59:58
*
*/
public class TicketHunter {
private static final String myStore = "F:/FUN/ticket/t1547";//store文件
private static final String codeImg = "F:/FUN/ticket/chkcode/1.png";//登陆验证码生成文件
private static final String loginUserName = "XXXXXXXXXX@126.com";//12306账号
private static final String loginPassWord = "**********";//12306密码
public static void main(String[] args) throws Exception {
//创建HttpClient对象
HttpClient httpclient = zeroGoal();
//获得sessionId
oneGoal(httpclient);
//获得验证码
twoGoal(httpclient);
//获得登陆随机数
String threeResult = threeGoal(httpclient);
//登陆
fourGoal(httpclient,threeResult);
//查询车票
fiveGoal(httpclient);
}
/**
* @Title: fiveGoal
* @Description: TODO
* @author renfy
* @date 2013-7-25 上午11:59:09
* @param @param httpclient
*
*/
private static List<String> fiveGoal(HttpClient httpclient) throws Exception{
long times = new Date().getTime() + 1l * 24 * 60 * 60 * 1000;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String trainDate = "2013-09-11";//查询的火车票时间
trainDate = sdf.format(new Date(times));
System.out.println("查询的火车票日期:" + trainDate);