12306火车时刻表查询接口

冯福
2023-12-01

摘要

本文讲解12306网站接口查询火车时刻表信息
接口中用到的方法在文章最后

一、 根据车次查询内部火车编号

接口:https://search.12306.cn/search/v1/train/search
参数: keyword, date
请求类型: GET

/**
 * 根据车次名称查询列车编号
 * @param keyword 车次名
 * @param date 发车日期 (格式: yyyyMMdd)
 */
private List<TrainInfo> searchTrainInfos(String keyword, String date) {
	date = checkTrainSearchDate(date, "yyyyMMdd")
    String response = HttpUtils.sendGet("https://search.12306.cn/search/v1/train/search",
    		"keyword=" + keyword.trim() + "&date=" + date);
    JSONObject object = new JSONObject(response);
    if (object.optBoolean("status")) {
        JSONArray data = object.optJSONArray("data");
        if (data != null) {
            return JSON.parseArray(data.toString(), TrainInfo.class);
        }
    }
    throw new RuntimeException("未查询到车次信息. " + object.optString("errorMsg"));
}

列车信息实体类 TrainInfo

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 车次信息
 */
@Data
public class TrainInfo implements Serializable {
    private String date ;

    @JsonProperty(value = "from_station")
    private String fromStation ;

    @JsonProperty(value = "station_train_code")
    private String stationTrainCode ;

    @JsonProperty(value = "to_station")
    private String toStation ;

    @JsonProperty(value = "total_num")
    private String totalNum ;

    @JsonProperty(value = "train_no")
    private String trainNo ;
}

二、 根据列车编号查询时刻表信息

接口:https://kyfw.12306.cn/otn/queryTrainInfo/query
参数: trainNo, date
请求类型: GET

/**
 * 根据列车编号查询时刻表信息
 * @param trainNo 列车编号, 是上面接口返回数据中心的train_no
 * @param date 查询日期 (格式: yyyy-MM-dd)
 */
private List<TrainStation> searchTrainStations(String trainNo, String date) {
    if (StringUtils.isBlank(trainNo)) {
        throw new RuntimeException("未查询到相关列车信息");
    }
    date = checkTrainSearchDate(date, "yyyy-MM-dd");
    String response = HttpUtils.sendGet("https://kyfw.12306.cn/otn/queryTrainInfo/query",
            "leftTicketDTO.train_no="+ trainNo +"&leftTicketDTO.train_date="+date +"&rand_code=");
    JSONObject object = new JSONObject(response);
    if (object.optBoolean("status")) {
        JSONObject jsonObject = object.optJSONObject("data");
        if (jsonObject != null) {
            JSONArray array = jsonObject.optJSONArray("data");
            if (array != null) {
                List<TrainStation> list = JSON.parseArray(array.toString(), TrainStation.class);
                TrainStation station = list.get(0);
                list.forEach(i -> {
                    i.setStartStationName(station.getStartStationName());
                    i.setEndStationName(station.getEndStationName());
                    i.setTrainClassName(station.getTrainClassName());
                });
                return list;
            }
        }
    }
    throw new RuntimeException("未查询到相关列车信息. " + JSON.toJSONString(object.opt("messages")));
}

列车到站时刻详情


import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * 列车到站时刻详情
 * @author xyang
 * @date 2021-08-02 9:15
 */
@Data
public class TrainStation implements Serializable {
    /** 车站名称 */
    @JsonProperty(value = "station_name")
    private String stationName;

    /** 列车类型 */
    @JsonProperty(value = "train_class_name")
    private String trainClassName;

    /** 是否起始站 (y/n)*/
    @JsonProperty(value = "is_start")
    private String isStart;

    @JsonProperty(value = "service_type")
    private String serviceType;

    /** 起始站点名称 */
    @JsonProperty(value = "start_station_name")
    private String startStationName;

    /** 终点站名称 */
    @JsonProperty(value = "end_station_name")
    private String endStationName;

    /** 发车时间 */
    @JsonProperty(value = "start_time")
    private String startTime;

    /** 到站时间 */
    @JsonProperty(value = "arrive_time")
    private String arriveTime;

    /** 车次编号 */
    @JsonProperty(value = "station_train_code")
    private String stationTrainCode;

    /** 到站顺序 */
    @JsonProperty(value = "station_no")
    private String stationNo;

    @JsonProperty(value = "wz_num")
    private String wzNum;

    /** 到站历时天数 (0:当天, 1:次日, 2:第三日, ....) */
    @JsonProperty(value = "arrive_day_diff")
    private Integer arriveDayDiff;

    /** 历时时长 */
    @JsonProperty(value = "running_time")
    private String runningTime;

    /** 历时 (当日到达, 次日到达...)*/
    @JsonProperty(value = "arrive_day_str")
    private String arriveDayStr;

    @JsonProperty(value = "OT")
    private List<Object> ot;
}

其他

时间校验 checkTrainSearchDate

/**
 * 校验时间格式是否正确, 并将字符串转为日期(防止日期格式未按要求)
 * @param date 日期字符串
 * @param format 描述日期和时间格式的模式
 */
private String checkTrainSearchDate(String date, String format) {
    if(StringUtils.isBlank(date)) {
        throw new RuntimeException("查询日期不能为空!");
    }
    if(StringUtils.isBlank(format)) {
        throw new RuntimeException("日期格式化参数错误!");
    }
    Date parseDate = DateUtils.parseDate(date);
    if (parseDate == null) {
        throw new RuntimeException("查询日期格式校验错误!");
    }

    if (DateUtils.truncatedCompareTo(parseDate, new Date(), Calendar.DATE) < 0) {
        throw new RuntimeException("查询日期不能早于当前日期");
    }
    return DateUtils.dateFormat(parseDate, format);
}

时间工具类 DateUtils

// 时间工具类
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	public static String YYYY = "yyyy";
    public static String YYYY_MM = "yyyy-MM";
    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

	/**
     * 格式化日期
     */
	public static String dateFormat(Date date,String format) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            if(ObjectUtils.isEmpty(date)){
                date = new Date();
            }
            return simpleDateFormat.format(date);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("日期格式化失败");
        }
    }
    
	/**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try{
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e){
            return null;
        }
    }
}

**通用http发送方法 HttpUtils **


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

/**
 * 通用http发送方法
 *
 * @author ruoyi
 */
public class HttpUtils {
    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    /**
     * 向指定 URL 发送GET方法的请求
     *
     * @param url   发送请求的 URL
     * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static String sendGet(String url, String param) {
        return sendGet(url, param, "UTF-8");
    }

    /**
     * 向指定 URL 发送GET方法的请求
     *
     * @param url         发送请求的 URL
     * @param param       请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @param contentType 编码类型
     * @return 所代表远程资源的响应结果
     */
    public static String sendGet(String url, String param, String contentType) {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            log.info("sendGet - {}", urlNameString);
            URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            log.info("recv - {}", result);
        } catch (ConnectException e) {
            log.error("调用HttpUtils.sendGet ConnectException, url=" + url + ",param=" + param, e);
        } catch (SocketTimeoutException e) {
            log.error("调用HttpUtils.sendGet SocketTimeoutException, url=" + url + ",param=" + param, e);
        } catch (IOException e) {
            log.error("调用HttpUtils.sendGet IOException, url=" + url + ",param=" + param, e);
        } catch (Exception e) {
            log.error("调用HttpsUtil.sendGet Exception, url=" + url + ",param=" + param, e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception ex) {
                log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
            }
        }
        return result.toString();
    }

    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url   发送请求的 URL
     * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     */
    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try {
            log.info("sendPost - {}", url);
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            conn.setRequestProperty("Accept-Charset", "utf-8");
            conn.setRequestProperty("contentType", "utf-8");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            out = new PrintWriter(conn.getOutputStream());
            out.print(param);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
            log.info("recv - {}", result);
        } catch (ConnectException e) {
            log.error("调用HttpUtils.sendPost ConnectException, url=" + url + ",param=" + param, e);
        } catch (SocketTimeoutException e) {
            log.error("调用HttpUtils.sendPost SocketTimeoutException, url=" + url + ",param=" + param, e);
        } catch (IOException e) {
            log.error("调用HttpUtils.sendPost IOException, url=" + url + ",param=" + param, e);
        } catch (Exception e) {
            log.error("调用HttpsUtil.sendPost Exception, url=" + url + ",param=" + param, e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                log.error("调用in.close Exception, url=" + url + ",param=" + param, ex);
            }
        }
        return result.toString();
    }

}
 类似资料: