Пример #1
0
        /// <summary>
        /// 从文件中加载所有的站点
        /// </summary>
        /// <returns></returns>
        public static StationEntity[] LoadStationNames()
        {
            StationEntity[] result = null;
            if (!File.Exists(stationNameFilePath))
            {
                var url  = "https://kyfw.12306.cn/otn/leftTicket/init";
                var html = HtmlHelp.GetHtmlString(url, Encoding.UTF8);
                if (html == "")
                {
                    throw new Exception("获取12306主页失败!");
                }
                var regTxt = "<script type=\"text/javascript\" src=\"(?'Url'/otn/resources/js/framework/station_name.js\\?station_version=[\\.0-9]+)\"\\s*xml:space=\"preserve\"></script>";
                var reg    = new Regex(regTxt, RegexOptions.Singleline);
                var m      = reg.Match(html);
                if (m == null || m.Groups["Url"].Value == "")
                {
                    throw new Exception("获取站点文件网址失败!");
                }

                var nameUrl = m.Groups["Url"].Value;
                //nameUrl = HtmlHelp.NeedHost(url, nameUrl);
                nameUrl = "https://kyfw.12306.cn" + nameUrl;
                var str = HtmlHelp.GetHtmlString(nameUrl, Encoding.UTF8);
                if (str == "")
                {
                    throw new Exception("获取站点文件失败!");
                }

                result = GetStationNames(str);
                if (result.Length <= 0)
                {
                    throw new Exception("站点数为零!");
                }

                var json = Newtonsoft.Json.JsonConvert.SerializeObject(result);
                HtmlHelp.SaveTxtFile(stationNameFilePath, json, false);
            }
            else
            {
                if (HtmlHelp.cookieString == "")
                {
                    var url = "https://kyfw.12306.cn/otn/leftTicket/init";
                    HtmlHelp.GetHtmlString(url, Encoding.UTF8);
                }

                var str = HtmlHelp.LoadTxtFile(stationNameFilePath);
                result = Newtonsoft.Json.JsonConvert.DeserializeObject <StationEntity[]>(str);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// 查询列车的路由
        /// </summary>
        /// <param name="trainNo"></param>
        /// <param name="startCode"></param>
        /// <param name="endCode"></param>
        /// <param name="time"></param>
        public static QueryTrainRouteResult QueryTrainRoute(string trainNo, string startCode, string endCode, string time)
        {
            var url = "https://kyfw.12306.cn/otn/czxx/queryByTrainNo?train_no={0}&from_station_telecode={1}&to_station_telecode={2}&depart_date={3}";

            url = string.Format(url, trainNo, startCode, endCode, time);

            var html = HtmlHelp.GetHtmlString(url, Encoding.UTF8);

            if (html == "")
            {
                throw new Exception("查询列车的路由失败");
            }

            try
            {
                var result = Newtonsoft.Json.JsonConvert.DeserializeObject <QueryTrainRouteResult>(html);
                return(result);
            }
            catch (Exception e)
            {
                throw new Exception("查询列车路由时反序列化失败!");
            }
        }
Пример #3
0
        /// <summary>
        /// 查询指定地点到目的地的所有符合的列车
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="time"></param>
        public static QueryTrainResultEntity QueryTrain(string startCode, string endCode, string time)
        {
            var url = "https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date={0}&leftTicketDTO.from_station={1}&leftTicketDTO.to_station={2}&purpose_codes=ADULT";

            url = string.Format(url, time, startCode, endCode);

            var html = HtmlHelp.GetHtmlString(url, Encoding.UTF8);

            if (html == "")
            {
                throw new Exception("查询列车失败!");
            }

            try
            {
                var result = Newtonsoft.Json.JsonConvert.DeserializeObject <QueryTrainResultEntity>(html);
                return(result);
            }
            catch (Exception e)
            {
                throw new Exception("查询列车时反序列化失败!");
            }
        }