Let's get straight to the point, here I mainly want to talk about the interfaces and data formats of the current 12306 functions. It is mainly divided into five modules: login, getting contacts, checking tickets, booking, and querying unfinished orders, as for some other interfaces that are not important, you can directly ignore them, such as detecting whether the verification code is correct, requesting the remaining number of tickets, etc., this article is written out, and you can study it yourself if you need it. 1. Log in Login is mainly to get the session and maintain communication with the server. There are two main steps to log in First, get the login verification code Method: GET Interface: https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew.do?module=login&rand=sjrand& Parameters: copy, the same Back: Binary picture stream Second Login Method: POST Interface: https://kyfw.12306.cn/otn/login/loginAysnSuggest Parameters: 1. loginUserDTO.user_name User name 2. randCode verification code 3. userDTO.password password Returns: json string, which can determine whether the login is successful 2. Get contacts Method: GET Interface: https://kyfw.12306.cn/otn/passengers/init Parameters: None Return: html file, you can find a variable in it, which is in json format and can be parsed into a user object 3. Ticket checking Method: GET Interface: https://kyfw.12306.cn/otn/leftTicket/query? Parameters: leftTicketDTO.train_date: Ticket check date leftTicketDTO.from_station: Starting Station (Alphabetical Code) leftTicketDTO.to_station: Destination station (alphabet code) purpose_codes:ADULT Return: Returns to JSON format and parses directly 4. Reservation The appointment is divided into 6 steps, one by one, interrelated First: Pre-submission Method: POST Interface: https://kyfw.12306.cn/otn/leftTicket/submitOrderRequest Parameter: secretStr: obtained from ticket checking (one for each train, and different each time, needs to be parsed in real time) train_date: Booking date back_train_date: Return date tour_flag: dc purpose_codes:ADULT query_from_station_name: Departure station query_to_station_name: Destination station undefined: an empty string Return: Returns the JSON format to determine whether the submission is successful Second: Get the corresponding parameters Method: POST Interface: https://kyfw.12306.cn/otn/confirmPassenger/initDc Parameters: _json_att: Empty string Return: html file, useful under parsing globalRepeatSubmitToken in the first few lines, and getting the value of the ticketInfoForPassengerForm variable from below, is in json format, from which purpose_codes, key_check_isChange, leftTicketStr, train_locatio{filter} n to prepare for the following commits Third: Get the submission verification code Method: GET Interface: https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew.do?module=passenger&rand=randp& Parameters: None Return: Returns the image binary stream, just like the login verification code Fourth: Check whether the submission can be confirmed Method: POST Interface: https://kyfw.12306.cn/otn/confirmPassenger/checkOrderInfo Parameters: cancel_flag:2 bed_level_order_num:000000000000000000000000000000 passengerTicketStr: Seat type, 0, ticket type, name, identity, phone, N (if there are more than one, separated by a comma) oldPassengerStr: Name, ID Type, ID Number, User Type randCode: Scheduled verification code tour_flag:dc _json_att: Empty string REPEAT_SUBMIT_TOKEN: Get it from the previous step Return: json format to determine whether it can be committed Fifth: Confirm submission Method: POST Interface: https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue Parameters:
passengerTicketStr: Seat type, 0, ticket type, name, identity, phone, N (if there are more than one, separated by a comma) oldPassengerStr: Name, ID Type, ID Number, User Type randCode: Scheduled verification code purpose_codes: Step 3 to get it key_check_isChange: Ibid leftTicketStr; Ibid train_locatio{filter}n; Ibid REPEAT_SUBMIT_TOKEN: Ibid _json_att: Empty string Return: json format, determine whether the submission is successful, return the waiting time, and call step 6 every certain period to return the processing result Sixth: Poll to get the submission results Method: GET Interface: https://kyfw.12306.cn/otn/confirmPassenger/queryOrderWaitTime? Parameters:
random: Random numbers tourFlag:dc REPEAT_SUBMIT_TOKEN: There is a third part to obtain _json_att: Empty string Return: json format, return the waiting time, if waitTime is less than 0, get the order information orderId, if it is greater than 0, continue polling 5. Inquire about unfinished orders Method: POST Interface: https://kyfw.12306.cn/otn/queryOrder/queryMyOrderNoComplete Parameters: _json_att: Empty string Returns: JSON format, contains order information, and parses directly
Speaking of which, the 5 functions are basically introduced, but now it is posted just to learn to use, and will not be maliciously attacked, moreover, the interface of the 12306 website changes very frequently, and the interface posted now may be changed tomorrow, so the important thing is to learn how to grab relevant interfaces and data, and analyze from the data to obtain the corresponding parameters and corresponding results. Here I mainly use firebug, the network analysis tool that comes with the chrom browser, as well as the fiddler and wireshark tools, and the instructions for using related tools, just look for it on the Internet. Finally, let's complain, the 12306 website uses CDN static caching technology, which will lead to the selection of corresponding servers according to load balancing in different regions, bandwidth, and load, that is to say, there are many 12306 servers across the country Each user may access the possible servers are different, but only static pages and js, css and pictures, etc., but each server will directly keep the session shared, that is, if you log in to one of the servers, on other servers The above is also equivalent to having landed. So it's best to be able to choose a server dynamically so that cache times are up to date, stress is minimal, and data is more real-time. In my implementation, if you don't query once, you will change the server IP once. As for the relevant technology, I will reveal it later. But the content of this article is enough for learning. You can see the specific IP address in the http://tool.chinaz.com/dns. |