C # If you want to read more than 12306 votes, you can directly use the Get string request without logging in.
Original article-My CSDN blog
The same method is used to read the fare information.
That is, read the page:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12404Q914-0.jpg "title =" balance 01.jpg "alt =" 204116807.jpg"/>
Use IE9 to capture query parameters:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/12404T261-1.jpg "title =" balance 02.jpg "alt =" 204149905.jpg"/>
Because the query method is GET, you can directly view the query url, as shown below:
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
There are four key parameters:
ORderRequest. train_date =, which indicates the ride date to be queried.
OrderRequest. from_station_telecode = GZQ, the departure station's Bureau code. GZQ refers to the Bureau code, indicating Guangzhou.
OrderRequest. to_station_telecode = XAY. This also refers to the Bureau code of the arrival station, and XAY refers to Xi'an.
OrderRequest. train_no = this parameter is optional. If you enter this parameter, it is not the number of trains, but the electric Report code of the trains.
The query url is changed:
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 is missing. This is the record code of the vehicle.
Then, how can I obtain the Bureau code? capture the code and there is a request in the interception of the query ticket page:
Http://dynamic.12306.cn/otsquery/js/common/favorite_name.js? Version = 1.55Of course, the version may change. The returned results are as follows:
@ Bji | Beijing | BJP | 0 @ sha | Shanghai | SHH | 1 @ tji | Tianjin | TJP | 2 @ cqi | Chongqing | CQW | 3 @ csh | Changsha | CSQ | 4 @ cch | Changchun | CCT | 5 @ cdu | Chengdu | CDW | 6 @ fzh | Fuzhou | FZS | 7 @ gzh | Guangzhou | GZQ | 8 @ gya | Guiyang | GIW .. ....
The uppercase part is the Bureau code. If you are interested, you can download it and save it again.
How does the telephone number of a vehicle come from? Of course, if you want to simply query the remaining tickets, you can ignore the telegraph parameters. If you want to query the remaining fare of a certain train, you can read the 12306 fare information in another article. The string that queries the fare information contains a parameter that is the telephone report code of the train. As follows:
Parent. mygrid. addRow (1, "2, K1168 ^ skbcx. jsp? Cxlx = cc & date = 20131126 & trainCode = k1168_6b000k0000801, Guangzhou ^ skbcx. jsp? Cxlx = czjgcc & zm = & date = 20131126 & stationName_passTrain = % E5 % B9 % BF % E5 % B7 % 9E ^ self, Xi'an ^ skbcx. jsp? Cxlx = czjgcc & zm = & date = 20131126 & stationName_passTrain = % E8 % A5 % BF % E5 % AE % 89 ^ self, --, 724/757, 408/421/436, --,: 27: 14, Haikou ^ skbcx. jsp? Cxlx = czjgcc & zm = & date = 20131126 & stationName_passTrain = % E6 % B5 % B7 % E5 % 8F % A3 ^ self, Xi'an ^ skbcx. jsp? Cxlx = czjgcc & zm = & date = 20131126 & stationName_passTrain = % E8 % A5 % BF % E5 % AE % 89 ^ self, fast, with ", 1 );
Specifically, trainCode = k1168_6b000k2017801 and the underscore (_) is the report code of the K1168 train. Therefore, you can use this article to query the remaining ticket for a certain train.
This query is relatively simple, but you need to obtain the Bureau codes of all stations in advance.
The implementation method is as follows:
Public static string DoGetTrainTicketLeft (string fromTime, string fromStation, string arriveStation, string trainTeleCode) {string result = string. empty; try {// 0-time 1-departure station 2-reached 3-train count 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 % 23 T % 23 K % 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 ;}
Call,
DoGetTrainTicketLeft ("2013-11-26", "GZQ", "XAY", "6b000k0000801 ");
As for filling in the last three parameters, you can find a way to download the station data on the Internet. This is because I used the official Railway Bureau data to implement this method. The database contains the station and station Bureau code data. The train telegraph code is obtained based on the fare query.
Call the preceding method and return the following data:
{"Datas": "0, <span id = 'id _ 6b000k0000801 'class = 'base _ txtdiv 'src ='/otsquery/images/tips/last.gif '> & nbsp; & nbsp; Xi'an & nbsp; <br> & nbsp; & nbsp;, --, 8,181, --, 156,198, --, "," time ":" 20:19 "}
Obviously, this is a json string. You can obtain the expected additional votes by performing corresponding truncation.