private static bool GetTask(Train train, string date, SeatsType seat, List<Contact> selectContactList) { ShowMessage(null, new List<Message>() { new Message("开始抢票...",Color.Yellow) }); var v = _12306Class.GetTask(train, date, seat,selectContactList); if (v != null && !v.IsCreate) { // MessageBox.Show(Program.mainForm, v.Message); ShowMessage(null,new List<Message>() { new Message("抢票失败..."+v.Message,Color.Red) }); } else { ShowMessage(null, new List<Message>() { new Message("抢票 ...", Color.Yellow), new Message(v.Message, Color.Red) }); } return v.IsCreate; }
public static CreateResponse GetTask(Train train, string date, SeatsType seat, List<Contact> selectContactList) { string seatType = ""; switch (seat) { case SeatsType.商务座: seatType = "9"; break; case SeatsType.特等座: seatType = "P"; break; case SeatsType.一等座: seatType = "M"; break; case SeatsType.二等座: seatType = "O"; break; case SeatsType.高级软卧: seatType = "5"; break; case SeatsType.软卧: seatType = "4"; break; case SeatsType.硬卧: seatType = "3"; break; case SeatsType.软座: seatType = "2"; break; case SeatsType.硬座: seatType = "1"; break; case SeatsType.无座: seatType = "1"; break; } StringBuilder passengerTicketStr = new StringBuilder(); StringBuilder oldPassengerStr = new StringBuilder(); foreach (Contact contact in selectContactList) { passengerTicketStr.AppendFormat("{0},0,1,{1},{2},{3},{4},N_", seatType, contact.Name, contact.IdTypeCode, contact.IdNo, contact.Mobile); oldPassengerStr.AppendFormat("{1},{2},{3},1_", seatType, contact.Name, contact.IdTypeCode, contact.IdNo, contact.Mobile); } Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add("secretStr", train.Id); dic.Add("train_date", date); dic.Add("tour_flag", "dc"); dic.Add("purpose_codes", "ADULT"); dic.Add("query_from_station_name", From.Name); dic.Add("query_to_station_name", To.Name); dic.Add("", ""); dic.Add("cancel_flag", "2"); dic.Add("bed_level_order_num", "000000000000000000000000000000"); dic.Add("passengerTicketStr", passengerTicketStr.ToString().TrimEnd('_')); dic.Add("oldPassengerStr", oldPassengerStr.ToString()); var codeResquest = new HttpHelper2().Post(GetTask_1_GetToken, dic, Encoding.UTF8, Encoding.UTF8, Referer: "https://kyfw.12306.cn/otn/leftTicket/init",cookies:Cookies , headers: new Dictionary<string, string>() { { "Origin", "https://kyfw.12306.cn" } }); if (codeResquest == null) { return new CreateResponse(){IsCreate = false,Message = "Errorl"}; } var serializer = new JavaScriptSerializer(); serializer.RegisterConverters(new[] { new DynamicJsonConverter() }); dynamic data = serializer.Deserialize<object>(codeResquest); if (data.status == null || !data.status || data.data == null || data.data.result == null) { try { return new CreateResponse() { IsCreate = false, Message = data.messages != null ? data.messages[0] ?? "未知错误" : "未知错误" }; } catch (Exception) { return new CreateResponse() { IsCreate = false, Message = data.errMsg ?? "未知错误" }; } } //Q6#BA6C4F23E49E84F96A07B8ECA37A9FF350DAD2E2F484AD96F61C2046#O007450669M0099501499019950025#1 Token token = new Token(); string[] tokens = data.data.result.Split('#');; token.Q = tokens[0]; token.LongToken = tokens[1]; token.ShortToken = tokens[2]; dic.Clear(); dic.Add("train_date", (Convert.ToDateTime(date).ToString("ddd MMM dd yyy ", DateTimeFormatInfo.InvariantInfo) + DateTime.Now.ToString("HH:mm:ss").Replace(":", "%3A") + " GMT%2B0800 (China Standard Time)").Replace(' ', '+')); dic.Add("train_no", train.TrainNo); dic.Add("stationTrainCode", train.StationTrainCode); dic.Add("seatType", seatType); dic.Add("fromStationTelecode", train.from_station_telecode); dic.Add("toStationTelecode", train.end_station_telecode); dic.Add("leftTicket", token.ShortToken); dic.Add("purpose_codes", "ADULT"); dic.Add("_json_att", ""); CookieCollection cookiesTemp = new CookieCollection(); cookiesTemp.Add(Cookies); cookiesTemp.Add(new Cookie("_jc_save_fromStation", escape(From.Name) + "%2C" + From.Code, "/otn") { Domain = "kyfw.12306.cn" }); cookiesTemp.Add(new Cookie("_jc_save_toStation", escape(To.Name) + "%2C" + To.Code, "/otn") { Domain = "kyfw.12306.cn" }); cookiesTemp.Add(new Cookie("_jc_save_fromDate", date, "/otn") { Domain = "kyfw.12306.cn" }); cookiesTemp.Add(new Cookie("_jc_save_toDate", Convert.ToDateTime(date).AddDays(15).ToString("yyyy-MM-dd"), "/otn") { Domain = "kyfw.12306.cn" }); cookiesTemp.Add(new Cookie("_jc_save_wfdc_flag", "dc", "/otn") { Domain = "kyfw.12306.cn" }); codeResquest = new HttpHelper2().Post(GetTask_2_GetQueueCount, dic, Encoding.UTF8, Encoding.UTF8, cookies: cookiesTemp, Referer: "https://kyfw.12306.cn/otn/leftTicket/init" , headers: new Dictionary<string, string>() { {"Origin", "https://kyfw.12306.cn"}, {"X-Requested-With", "XMLHttpRequest"} }); serializer = new JavaScriptSerializer(); serializer.RegisterConverters(new[] { new DynamicJsonConverter() }); data = serializer.Deserialize<object>(codeResquest); if (data.status == null || !data.status || data.data == null || data.data.ticket == null) { return new CreateResponse() { IsCreate = false, Message = data.messages != null ? data.messages[0] ?? "未知错误" : "未知错误" }; } token.ticket = data.data.ticket; string Code = ""; try { do { var response = HttpHelper.CreateGetHttpResponse(GetTask_3_Image + new Random().NextDouble().ToString(), null, "https://kyfw.12306.cn/otn/leftTicket/init", Cookies); Stream resStream = response.GetResponseStream();//得到验证码数据流 Bitmap bmp = new Bitmap(resStream);//初始化Bitmap图片 new Music(2).Play(); ImageFrom.GetImageFrom.Show(bmp); Code = ImageFrom.GetImageFrom.Code; } while (Code.Length != 4 && Program.mainForm.GetRunStatus()); } catch (Exception) { return new CreateResponse() { IsCreate = false, Message = data.data.errMsg ?? "未知错误" }; } //dic.Clear(); //dic.Add("randCode", Code); //dic.Add("rand", "sjrand"); //dic.Add("_json_att", ""); //codeResquest = new HttpHelper2().Post("https://kyfw.12306.cn/otn/passcodeNew/checkRandCodeAnsyn", // dic, Encoding.UTF8, Encoding.UTF8, Referer: "https://kyfw.12306.cn/otn/leftTicket/init", cookies: cookiesTemp // , headers: // new Dictionary<string, string>() // { // {"Origin", "https://kyfw.12306.cn"}, // {"X-Requested-With", "XMLHttpRequest"} // }); dic.Clear(); dic.Add("passengerTicketStr", System.Web.HttpUtility.UrlEncode((passengerTicketStr.ToString().TrimEnd('_'))).ToUpper()); dic.Add("oldPassengerStr", System.Web.HttpUtility.UrlEncode(oldPassengerStr.ToString()).ToUpper()); dic.Add("randCode", Code); dic.Add("purpose_codes", "ADULT"); dic.Add("key_check_isChange", token.LongToken); dic.Add("leftTicketStr", token.ShortToken); dic.Add("train_location", token.Q); dic.Add("_json_att", ""); cookiesTemp = new CookieCollection(); cookiesTemp.Add(Cookies); cookiesTemp.Add(new Cookie("_jc_save_fromStation", escape(From.Name) + "%2C" + From.Code, "/otn") { Domain = "kyfw.12306.cn" }); cookiesTemp.Add(new Cookie("_jc_save_toStation", escape(To.Name) + "%2C" + To.Code, "/otn") { Domain = "kyfw.12306.cn" }); cookiesTemp.Add(new Cookie("_jc_save_fromDate", date, "/otn") { Domain = "kyfw.12306.cn" }); cookiesTemp.Add(new Cookie("_jc_save_toDate", Convert.ToDateTime(date).AddDays(15).ToString("yyyy-MM-dd"), "/otn") { Domain = "kyfw.12306.cn" }); cookiesTemp.Add(new Cookie("_jc_save_wfdc_flag", "dc", "/otn") { Domain = "kyfw.12306.cn" }); codeResquest = new HttpHelper2().Post(GetTask_4_SubmitStatus, dic, Encoding.UTF8, Encoding.UTF8, Referer: "https://kyfw.12306.cn/otn/leftTicket/init", cookies: cookiesTemp , headers: new Dictionary<string, string>() { {"Origin", "https://kyfw.12306.cn"}, {"X-Requested-With", "XMLHttpRequest"} }); if (codeResquest == null) { return new CreateResponse() { IsCreate = false, Message = "Errorl" }; } serializer = new JavaScriptSerializer(); serializer.RegisterConverters(new[] { new DynamicJsonConverter() }); data = serializer.Deserialize<object>(codeResquest); if (data.status == null || !data.status || data.data == null || data.data.submitStatus == null) { try { return new CreateResponse() { IsCreate = false, Message = data.messages != null ? data.messages[0] ?? "未知错误" : "未知错误" }; } catch (Exception) { return new CreateResponse() { IsCreate = false, Message = data.data.errMsg ?? "未知错误" }; } } else if (data.data != null && data.data.submitStatus!=null) { if (data.data.submitStatus) { TickCute.ChangeTickCount(-1); new Music(1).Play(); Program.mainForm.SetLinkLabelText("订票成功!登录12306查看!<-"); return new CreateResponse() { IsCreate = true, Message = "订票成功!" }; } else { return new CreateResponse() { IsCreate = false, Message = "订票失败!" }; } } return new CreateResponse() { IsCreate = false, Message = data.messages != null ? data.messages[0] ?? "未知错误" : "未知错误" }; //else if (data.messages.Count <=0) //{ // return new LoginResponse() { IsLogined = true, Message = "", type = ErrorType.None, LoginName = userName }; //} }