Recently, I am setting up a WeChat public account, which involves querying train tickets. I used an interface found online before, but it could only find train timetables, and 12306 did not provide a dedicated interface for checking tickets. Today I suddenly remembered that I went directly to 12306 to query and grab the data packet returned by the query, so that I can get the train ticket information. Here I will take notes on the process of obtaining the remaining 12,306 votes.
First, I used 12306 on Firefox browser to check the remaining tickets. Open Firefox's Web console and select "Record request and response bodies" in the network
Then enter the address date information and click the query button on the web page, and you can see the address requested by the web page in the Web console:
It is the second item in the picture, which is the actual address that handles the event when you click the query button. Click it to see
Request URL, request header, response header and response body. The response body contains the train ticket information we need.
With this request URL, you can proceed to the actual code to operate. It can be found that the format of the URL is
The front is the address for processing the request, and the following parameters purpose_codes refer to adult tickets (AADULT), student tickets (try it yourself), queryDate is the date, and from_station and to_station are the departure and arrival stations as their names suggest. Here Beijing and Wuhan are represented as BJP and WHN respectively.
In the java code, you can directly write an https request to obtain the train ticket information packet.
Copy the code code as follows:
public static List<NewTrain> getmsg(String startCity,String endCity,int isAdult) throws Exception{
List<NewTrain> trains = new ArrayList<NewTrain>();
String sstartCity = CityCode.format(startCity);
String sendCity = CityCode.format(endCity);
TrustManager[] tm = {new MyX509TrustManager()};
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// Get the SSLSocketFactory object from the above SSLContext object
SSLSocketFactory ssf = sslContext.getSocketFactory();
String type = "ADULT";
if(isAdult == 1){
type = "0X00";
}
String urlStr = "https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes="+type+"&queryDate=2014-04-27&from_station="+sstartCity+"&to_station="+sendCity;
URL url = new URL(urlStr);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(ssf);
InputStreamReader in = new InputStreamReader(con.getInputStream(),"utf-8");
BufferedReader bfreader = new BufferedReader(in);
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = bfreader.readLine()) != null) {
sb.append(line);
}
System.out.println(sb.toString());
}
The cityCode.format() of this code is written by myself to convert the Chinese website name into a combination of letters. The following lines are about https requests. The URL is the URL just obtained. The output obtained after executing this code is as follows:
It is easy to see that these data are pieces of json data (I did a simple process and asked him to print them out one by one).
Since it is json data, it is easy to handle. By taking out a piece of data for analysis, you can analyze the meaning of the key value. I only analyzed a few key values that I need
Then just write a Train class to store the train ticket information for later display.
Copy the code code as follows:public class NewTrain {
private String to_station_name; //Arrival place
private String station_train_code; //train number
private String from_station_name; //Departure place
private String start_time; //Departure time
private String arrive_time; // arrival time
private String lishi; // takes time
private String zy_num; // Number of first-class seats
private String ze_num; // Number of second-class seats
private String swz_num; // Number of business seats
private String gr_num; // Number of premium soft sleepers
private String rw_num; // Number of soft sleepers
private String rz_num; // Number of soft seats
private String yw_num; // Number of hard sleepers
private String yz_num; // Number of hard seats
private String tz_num; // Number of special seats
private String wz_num; // No number of seats
}
The next work is very simple, put the json data into the Train class object.
Okay, the basic work is completed, the next step is to integrate the functions into the project.
A txt file containing a combination of Chinese website names and letters used (read the txt to obtain the combination of letters corresponding to the Chinese website name, some may be incomplete