/// <summary> /// 查票 /// </summary> /// <returns></returns> async Task QueryTicket() { string fromCode, fromName, toCode, toName; var date = dtDate.Value; if (!GetTeleCode(cbFrom.Text, out fromName, out fromCode) || !GetTeleCode(cbTo.Text, out toName, out toCode)) { MessageBox.Show(this, "哎呀,没有选择车站,逗我呢 o(╯□╰)o", "哎呀", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } _result = null; Exception exception = null; BeginOperation("正在查询...", 0); btnQueryTicket.Enabled = false; try { _result = await _context.TicketQueryService.QueryTicket(date, fromName, fromCode, toName, toCode, ckStudent.Checked); } catch (Exception ex) { exception = ex; } finally { EndOperation(); } if (_result != null) { stStatus.Text = string.Format("查询到 {0} 趟车次。", _result.Count); //绑定数据 bs.DataSource = _result; } else { bs.DataSource = null; stStatus.Text = "查询出错,错误信息:" + exception.Message; } }
/// <summary> /// 查票 /// </summary> /// <param name="date">出发日期</param> /// <param name="fromName">发站名</param> /// <param name="fromCode">发站电报码</param> /// <param name="toName">到站名</param> /// <param name="toCode">到站电报码</param> /// <param name="isStudent">是否是学生票</param> /// <returns></returns> public async Task<TicketQueryResult> QueryTicket(DateTime date, string fromName, string fromCode, string toName, string toCode, bool isStudent) { var client = ServiceContext.Session.NetClient; if (_queryUrl.IsNullOrEmpty()) { //没有查询地址,说明咱还没查询过。所以这里先加载一下引导页 await LoadInitPage(); } //如果还是没有找到查询页地址,则报错 if (_queryUrl.IsNullOrEmpty()) { throw new Exception("无法找到查询页地址"); } //构造查询字符串 var queryparam = string.Format("?leftTicketDTO.train_date={0}&leftTicketDTO.from_station={1}&leftTicketDTO.to_station={2}&purpose_codes={3}", date.ToString("yyyy-MM-dd"), fromCode, toCode, (isStudent ? "0X00" : "ADULT")); //如果需要发送日志,则发送 if (_saveQueryLog) { client.Create<string>(HttpMethod.Get, "https://kyfw.12306.cn/otn/leftTicket/log" + queryparam, "https://kyfw.12306.cn/otn/leftTicket/init" ).SendAsync(false); } //发送查询请求 var ctx = client.Create<WebResponseResult<QueryTicketResultData>>( HttpMethod.Get, "https://kyfw.12306.cn/otn/" + _queryUrl + queryparam, "https://kyfw.12306.cn/otn/leftTicket/init" ); await ctx.SendTask(); if (!ctx.IsValid()) { throw ctx.Exception ?? new Exception("未能查询。"); } if (ctx.Result.Data == null) { //服务器返回错误信息 throw new Exception(ctx.Result.GetErrorMessage().DefaultForEmpty("未知错误信息")); } //生成结果 var result = new TicketQueryResult() { QueryFromStationCode = fromCode, QueryFromStationName = fromName, QueryToStationCode = toCode, QueryToStationName = toName, QueryDate = date }; result.AddRange(ctx.Result.Data.Select(s => new TicketQueryResultItem(date, s))); return result; }
/// <summary> /// 查票 /// </summary> /// <param name="date">出发日期</param> /// <param name="fromName">发站名</param> /// <param name="fromCode">发站电报码</param> /// <param name="toName">到站名</param> /// <param name="toCode">到站电报码</param> /// <param name="isStudent">是否是学生票</param> /// <returns></returns> public async Task <TicketQueryResult> QueryTicket(DateTime date, string fromName, string fromCode, string toName, string toCode, bool isStudent) { var client = ServiceContext.Session.NetClient; if (_queryUrl.IsNullOrEmpty()) { //没有查询地址,说明咱还没查询过。所以这里先加载一下引导页 await LoadInitPage(); } //如果还是没有找到查询页地址,则报错 if (_queryUrl.IsNullOrEmpty()) { throw new Exception("无法找到查询页地址"); } //构造查询字符串 var queryparam = string.Format("?leftTicketDTO.train_date={0}&leftTicketDTO.from_station={1}&leftTicketDTO.to_station={2}&purpose_codes={3}", date.ToString("yyyy-MM-dd"), fromCode, toCode, (isStudent ? "0X00" : "ADULT")); //如果需要发送日志,则发送 if (_saveQueryLog) { await client.Create <string>(HttpMethod.Get, "https://kyfw.12306.cn/otn/leftTicket/log" + queryparam, "https://kyfw.12306.cn/otn/leftTicket/init" ).SendAsync(); } //发送查询请求 var ctx = client.Create <WebResponseResult <QueryTicketResultData> >( HttpMethod.Get, "https://kyfw.12306.cn/otn/" + _queryUrl + queryparam, "https://kyfw.12306.cn/otn/leftTicket/init" ); await ctx.SendAsync(); if (!ctx.IsValid()) { throw ctx.Exception ?? new Exception("未能查询。"); } if (ctx.Result.Data == null) { //服务器返回错误信息 throw new Exception(ctx.Result.GetErrorMessage().DefaultForEmpty("未知错误信息")); } //生成结果 var result = new TicketQueryResult() { QueryFromStationCode = fromCode, QueryFromStationName = fromName, QueryToStationCode = toCode, QueryToStationName = toName, QueryDate = date }; result.AddRange(ctx.Result.Data.Select(s => new TicketQueryResultItem(date, s))); return(result); }