读取余票也是用同样的方式(读取票价信息)
也就是该页面。
使用IE9捕获查询时的参数:
因为这个查询是GET方式所以直接能看到查询的url,如下: http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryLeftTicket&orderRequest.train_date=2013-11-25&orderRequest.from_station_telecode=GZQ&orderRequest.to_station_telecode=XAY&orderRequest.train_no=&trainPassType=QB&trainClass=QB%23D%23Z%23T%23K%23QT%23&includeStudent=00&seatTypeAndNum=&orderRequest.start_time_str=00%3A00--24%3A00
这里有四个关键参数:
orderRequest.train_date=2013-11-25,指需要查询的乘车日期。
orderRequest.from_station_telecode=GZQ,出发站的局码,GZQ是指局码,表示广州。
orderRequest.to_station_telecode=XAY,同样这个是指到达站的局码,XAY表示西安。
orderRequest.train_no=这个参数是可选的,如果填的话,不是填车次,而是车次的电报码。
如图查询,加上列车车次,
那么查询url则变为:
http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryLeftTicket&orderRequest.train_date=2013-11-25&orderRequest.from_station_telecode=GZQ&orderRequest.to_station_telecode=XAY&orderRequest.train_no=6c0000G83201&trainPassType=QB&trainClass=QB%23D%23Z%23T%23K%23QT%23&includeStudent=00&seatTypeAndNum=&orderRequest.start_time_str=00%3A00--24%3A00
多了6c0000G83201,这个就是车次的电报码。
parent.mygrid.addRow(1,"2,K1168^skbcx.jsp?cxlx=cc&date=20131126&trainCode=K1168_6b000K116801,广州^skbcx.jsp?cxlx=czjgcc&zm=&date=20131126&stationName_passTrain=%E5%B9%BF%E5%B7%9E^self,西安^skbcx.jsp?cxlx=czjgcc&zm=&date=20131126&stationName_passTrain=%E8%A5%BF%E5%AE%89^self,--,--,--,--,--,724/757,408/421/436,--,240,08:57,12:11,27:14,海口^skbcx.jsp?cxlx=czjgcc&zm=&date=20131126&stationName_passTrain=%E6%B5%B7%E5%8F%A3^self,西安^skbcx.jsp?cxlx=czjgcc&zm=&date=20131126&stationName_passTrain=%E8%A5%BF%E5%AE%89^self,快速,有",1);
其中 trainCode=K1168_6b000K116801,下划线后的就是K1168次列车的电报码,所以如果针对某一列车查询余票 则结合该文章使用。
public static string DoGetTrainTicketLeft(string fromTime, string fromStation,string arriveStation,string trainTeleCode)
{
string result = string.Empty;
try
{
//0-时间 1-出发站 2-达到 3-车次
string trainTicketLeftUrl =
@"http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryLeftTicket&orderRequest.train_date={0}&orderRequest.from_station_telecode={1}&orderRequest.to_station_telecode={2}&orderRequest.train_no={3}&trainPassType=QB&trainClass=QB%23D%23Z%23T%23K%23QT%23&includeStudent=00&seatTypeAndNum=&orderRequest.start_time_str=00%3A00--24%3A00";
trainTicketLeftUrl = string.Format(trainTicketLeftUrl, fromTime, fromStation, arriveStation, trainTeleCode);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(trainTicketLeftUrl);
request.Accept = @"application/json, text/javascript, */*";
request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
request.Referer = @"http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=init";
request.ContentType = @"application/x-www-form-urlencoded";
request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
}
catch { }
return result;
}
调用,