コード例 #1
0
ファイル: TicketService.cs プロジェクト: wyrover/Ticket12306
 public TicketInfo GetTicketInfo()
 {
     try
     {
         TicketInfo ti     = null;
         HttpClient client = new HttpClient(Cookies);
         if (StaticValues.Proxy != "")
         {
             client.Proxy = new WebProxy(StaticValues.Proxy);
         }
         string url         = "https://kyfw.12306.cn/otn/confirmPassenger/initDc";
         string result      = client.Get(url, "kyfw.12306.cn", "https://kyfw.12306.cn", "https://kyfw.12306.cn/otn/leftTicket/init");
         var    pTicketInfo = HtmlHelper.GetContent(result, "'leftDetails':\\[", "],");
         pTicketInfo = Regex.Unescape(pTicketInfo).Replace("'", "");
         string[] list = pTicketInfo.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
         if (list.Length > 0)
         {
             ti = new TicketInfo();
             List <TickePriceInfo> tpiList = new List <TickePriceInfo>();
             for (int i = 0; i < list.Length; i++)
             {
                 string trainTicketInfoStr = list[i];
                 trainTicketInfoStr = trainTicketInfoStr.Replace("(", "#").Replace(")", "#");
                 string[]       priceArray = trainTicketInfoStr.Split("#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                 TickePriceInfo tpi        = new TickePriceInfo();
                 tpi.SeatType  = priceArray[0];
                 tpi.Price     = priceArray[1];
                 tpi.TicektCnt = priceArray[2];
                 tpiList.Add(tpi);
             }
             ti.TicketList = tpiList;
             ti.Token      = HtmlHelper.GetContent(result, "globalRepeatSubmitToken", ";").Replace("=", "").Replace("'", "").Trim();
             string leftTicket = HtmlHelper.GetContent(result, "'queryLeftTicketRequestDTO':", "}");
             leftTicket = leftTicket + "}";
             QueryLeftTicketRequestDTO leftTicketRequst = JsonHelper.FromJson <QueryLeftTicketRequestDTO>(leftTicket);
             //日期转换
             string trainDate = leftTicketRequst.train_date;
             leftTicketRequst.train_date = trainDate.Substring(0, 4) + "-" + trainDate.Substring(4, 2) + "-" + trainDate.Substring(6);
             var key_check_isChange = HtmlHelper.GetContent(result, "'key_check_isChange':", ",").Replace("'", "");
             var _temp = HtmlHelper.GetContent(result, "'queryLeftTicketRequestDTO':", "};");
             _temp = _temp + "};";
             var train_location = HtmlHelper.GetContent(_temp, "'train_location':", "};").Replace("'", "");
             ti.CheckKey       = key_check_isChange;
             ti.TrainLocation  = train_location;
             ti.LeftTicketInfo = leftTicketRequst;
         }
         return(ti);
     }
     catch
     {
         return(null);
     }
 }
コード例 #2
0
 public TicketInfo GetTicketInfo()
 {
     try
     {
         TicketInfo ti = null;
         HttpClient client = new HttpClient(Cookies);
         if (StaticValues.Proxy != "")
         {
             client.Proxy = new WebProxy(StaticValues.Proxy);
         }
         string url = "https://kyfw.12306.cn/otn/confirmPassenger/initDc";
         string result = client.Get(url, "kyfw.12306.cn", "https://kyfw.12306.cn", "https://kyfw.12306.cn/otn/leftTicket/init");
         var pTicketInfo = HtmlHelper.GetContent(result, "'leftDetails':\\[", "],");
         pTicketInfo = Regex.Unescape(pTicketInfo).Replace("'", "");
         string[] list = pTicketInfo.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
         if (list.Length > 0)
         {
             ti = new TicketInfo();
             List<TickePriceInfo> tpiList = new List<TickePriceInfo>();
             for (int i = 0; i < list.Length; i++)
             {
                 string trainTicketInfoStr = list[i];
                 trainTicketInfoStr = trainTicketInfoStr.Replace("(", "#").Replace(")", "#");
                 string[] priceArray = trainTicketInfoStr.Split("#".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                 TickePriceInfo tpi = new TickePriceInfo();
                 tpi.SeatType = priceArray[0];
                 tpi.Price = priceArray[1];
                 tpi.TicektCnt = priceArray[2];
                 tpiList.Add(tpi);
             }
             ti.TicketList = tpiList;
             ti.Token = HtmlHelper.GetContent(result, "globalRepeatSubmitToken", ";").Replace("=", "").Replace("'", "").Trim();
             string leftTicket = HtmlHelper.GetContent(result, "'queryLeftTicketRequestDTO':", "}");
             leftTicket = leftTicket + "}";
             QueryLeftTicketRequestDTO leftTicketRequst = JsonHelper.FromJson<QueryLeftTicketRequestDTO>(leftTicket);
             //日期转换
             string trainDate = leftTicketRequst.train_date;
             leftTicketRequst.train_date = trainDate.Substring(0, 4) + "-" + trainDate.Substring(4, 2) + "-" + trainDate.Substring(6);
             var key_check_isChange = HtmlHelper.GetContent(result, "'key_check_isChange':", ",").Replace("'", "");
             var _temp = HtmlHelper.GetContent(result, "'queryLeftTicketRequestDTO':", "};");
             _temp = _temp + "};";
             var train_location = HtmlHelper.GetContent(_temp, "'train_location':", "};").Replace("'", "");
             ti.CheckKey = key_check_isChange;
             ti.TrainLocation = train_location;
             ti.LeftTicketInfo = leftTicketRequst;
         }
         return ti;
     }
     catch
     {
         return null;
     }
 }