Mini tool: train ticket query, tool: train ticket
Today, when I got another train ticket, it was dumbfounded every time I grabbed the ticket. So I wrote a small tool to help myself query the train ticket. If there is a ticket, I will send myself an email asking me to buy it.
I. Preparations
Using tools such as firebug, we can obtain the Get request called when we click query.
Request address: https://kyfw.12306.cn/otn/leftTicket/query? LeftTicketDTO. train_date = 2017-09-01 & leftTicketDTO. from_station = TJP & leftTicketDTO. to_station = XHP & purpose_codes = ADULT analyze the parameters and obtain the following results:
- LeftTicketDTO. from_station = TJP Departure Site
- LeftTicketDTO. to_station = XHP arrival site
- LeftTicketDTO. train_date = departure date
- Purpose_codes = ADULT passenger type
Next we will analyze the returned json. The returned json is as follows:
{& Quot; validateMessagesShowId & quot;: & quot; _ validatorMessage & quot;, & quot; status & quot;: true & quot; httpstatus & quot;: 200, & quot; data & quot;: {& quot; result & quot ": ["%%2b % %%%%%%%2bccneag216pdgo % 2Fv % 2F0PIrQTTK % %%%0aj278lrm % 3D | reservation | 26policy5160f | Y516 | VVP | ZMP | TXP | XHP | Y | rC9CEREwzGPqGayEjfr9YPwYtGX % large | 20170901 | 3 | P2 | 11 | 19 | 0 | 0 | none | Yes | | 101020 | 112 ", "%%0ahr6l % 2BAgyc6ca % %%%%%2fajp3p % %%%%%%%%%| reservation | 2536611360j | 1136 | TJP | WXC | TJP | XHP | | Y | %2bfk6ujtmx5mylhgglnbc%zdc % 3D | 20170901 | 3 | P2 | 01 | 04 | 0 | 0 | none | Yes | none | | 10401030 | 1413 ", "%0a1gt % 2 %%%%%%%%2b3h % 0AC7Kx4hnW5HXZ | reservation | 25366k8880h | K888 | TJP | XNO | TJP | XHP | Y | uwEONiPztkLymI5okpF % California % 3D | 20170901 | 3 | PB | 01 | 04 | 0 | none | Yes | no | Yes | ||| | 10401030 | 1413 "], "flag": "1", "map": {"TXP": "Tianjin West", "XHP": "Xuanhua", "TJP": "Tianjin "}}, "messages": [], "validateMessages ":{}}
Webpage display:
Taking the Y516 vehicle count data as an example, we mainly clarify the meanings of the following content: "percent % 2B % percent % 2Fv % 2F0PIrQTTK % percent % 0AJ278LrM % 3D | reservation | 260000Y5160F | Y516 | VVP | ZMP | TXP | XHP | | Y | rC9CEREwzGPqGayEjfr9YPwYtGX % California | 20170901 | 3 | P2 | 11 | 19 | 0 | 0 | none | Yes | | 101020 | 112 "observe this string, it can be found that there is a certain pattern. You can use | to cut the string, and then analyze the cut content; analysis ...... after several data comparisons, the following conclusions are drawn:
- OSIL3O3WIe6kfzF42Zlz % 2B % latest % 2BCCNeAG216PdgO % 2Fv % 2F0PIrQTTK % latest % 0AJ278LrM % 3D | unknown content
- Y | whether there are votes N: no votes Y: Yes
- RC9CEREwzGPqGayEjfr9YPwYtGX % 2B45X6aKXyL0VaY0Bauu4t | unknown content
- 20170901 | unknown content
Now we have almost finished our preparations.
2. write code
The current task is to obtain data, the Code is as follows:
/// <Summary> // obtain the ticket query data /// </summary> /// <param name = "goTime"> travel date </param> /// <param name = "fromStationCode"> origin Code </param> // <param name = "toStationCode"> origin Code </param> /// <param name = "errorMsg"> error message </param> // <returns> </returns> public TicketSearchJson GetSearchTicketData (DateTime goTime, string fromStationCode, string toStationCode, ref string errorMsg) {string srcString = string. empty; try {using (WebClient client = new WebClient () {string url = string. format (" https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date= {0} & leftTicketDTO. from_station = {1} & leftTicketDTO. to_station = {2} & purpose_codes = ADULT ", goTime. toString ("yyyy-MM-dd"), fromStationCode, toStationCode); Uri uri = new Uri (url); ServicePointManager. serverCertificateValidationCallback = ValidateServerCertificate; byte [] responseData = client. downloadData (uri); srcString = Encoding. UTF8.GetString (responseData); TicketSearchJson ticketSearch = JsonConvert. deserializeObject <TicketSearchJson> (srcString); return ticketSearch ;}} catch (Exception ex) {errorMsg = ex. message; return null ;}} /// <summary> /// server certificate callback method /// </summary> /// <param name = "sender"> </param> /// <param name = "certificate"> </param> // <param name = "chain"> </param> // <param name = "sslPolicyErrors"> </param> /// <returns> </returns> private bool ValidateServerCertificate (object sender, x509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) {return true ;}
The email sending function can be seen in this blog: Gadgets: email sending
Iii. Running
Do not call interfaces too frequently !!!