using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;namespace TestQueryTrainTicketPrice
{
public class TrainTicketPriceQuery
{
private static string setCookie = string.Empty; /// <summary>
/// </summary>
public static string DoGetTrainTicketLeft(string trainTeleCode,string fromtime,string rand_Code)
{
string result = string.Empty;
try
{
//0-车次 1-时间 2-验证码
string trainTicketLeftUrl =@"https://kyfw.12306.cn/otn/queryTrainInfo/query?leftTicketDTO.train_no={0}&leftTicketDTO.train_date={1}&rand_code={2}"; trainTicketLeftUrl = string.Format(trainTicketLeftUrl, trainTeleCode,fromtime ,rand_Code );
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(trainTicketLeftUrl);
request.Accept = @"application/json, text/javascript, */*";
request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0))";
request.Referer = @"https://kyfw.12306.cn/otn/queryTrainInfo/init";
request.ContentType = @"application/json;charset=UTF-8";
request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
}
catch { }
return result;
}
/// <summary>
/// 票价查询验证码
/// </summary>
/// <returns></returns>
public static string DoGetTrainTicketPriceRandCodeImage()
{
string resultImage = string.Empty; try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew.do?module=login&rand=sjrand"); request.Accept = @"image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5"; request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; request.Referer = @"https://kyfw.12306.cn/otn/queryTrainInfo/init"; request.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
setCookie = response.GetResponseHeader("Set-cookie"); setCookie = Regex.Replace(setCookie, "path(?:[^,]+),?", "", RegexOptions.IgnoreCase); using (Stream reader = response.GetResponseStream())
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, new Random().Next(10000, 99999) + @"queryTicketPrice.JPEG");
using (FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
reader.CopyTo(file);
} resultImage = path; }
}
}
catch { } return resultImage;
}
}
}
这是函数
using System.IO;
using System.Net;
using System.Text.RegularExpressions;namespace TestQueryTrainTicketPrice
{
public class TrainTicketPriceQuery
{
private static string setCookie = string.Empty; /// <summary>
/// </summary>
public static string DoGetTrainTicketLeft(string trainTeleCode,string fromtime,string rand_Code)
{
string result = string.Empty;
try
{
//0-车次 1-时间 2-验证码
string trainTicketLeftUrl =@"https://kyfw.12306.cn/otn/queryTrainInfo/query?leftTicketDTO.train_no={0}&leftTicketDTO.train_date={1}&rand_code={2}"; trainTicketLeftUrl = string.Format(trainTicketLeftUrl, trainTeleCode,fromtime ,rand_Code );
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(trainTicketLeftUrl);
request.Accept = @"application/json, text/javascript, */*";
request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0))";
request.Referer = @"https://kyfw.12306.cn/otn/queryTrainInfo/init";
request.ContentType = @"application/json;charset=UTF-8";
request.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
}
catch { }
return result;
}
/// <summary>
/// 票价查询验证码
/// </summary>
/// <returns></returns>
public static string DoGetTrainTicketPriceRandCodeImage()
{
string resultImage = string.Empty; try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew.do?module=login&rand=sjrand"); request.Accept = @"image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5"; request.UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; request.Referer = @"https://kyfw.12306.cn/otn/queryTrainInfo/init"; request.Method = "GET"; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
setCookie = response.GetResponseHeader("Set-cookie"); setCookie = Regex.Replace(setCookie, "path(?:[^,]+),?", "", RegexOptions.IgnoreCase); using (Stream reader = response.GetResponseStream())
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, new Random().Next(10000, 99999) + @"queryTicketPrice.JPEG");
using (FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
reader.CopyTo(file);
} resultImage = path; }
}
}
catch { } return resultImage;
}
}
}
这是函数
解决方案 »
- c# 使用Tchart 做3D曲面图,有人会吗?
- 如何在vs2008中使用frameset?
- AXRichtextbox插入图片失真和粘贴Excel表格表格格式丢失问题
- winform中richtextbox的字体颜色设置
- vb6 写的一个 classmodulel 编译成dll,如何在c#下调用该dll?
- [求助]代码实现VSS下载文件(重点文件的修改日期)
- 怎么画柱状图?用什么控件啊?
- 在CSDN里这个问题我发过7个帖子,没有人能回答,希望今天能得到大家帮助-求一思路
- httpwebrequest获取cookie不全的问题(有图有代码,目前无真相...)
- 如图,这样的一个坐标区域是怎么绘制的?
- 未能加载文件或程序集“MySql.Data, Version=6.6.5.0, Culture=neutral,........
- CLR里面有么有一种数组类型叫做SzArray?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace TestQueryTrainTicketPrice
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
} void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
string date = DateTime.Now.AddDays(0).ToString(@"yyyy-MM-dd"); this.txtDate.Text = date; InitialRandCode();
} private void InitialRandCode()
{
//初始化验证码
string randCodeImageUrl = TrainTicketPriceQuery.DoGetTrainTicketPriceRandCodeImage(); if (!string.IsNullOrEmpty(randCodeImageUrl))
{
ImageSource src = (ImageSource)(new ImageSourceConverter().ConvertFromString(randCodeImageUrl)); this.imageRandCode.Source = src;
}
} /// <summary>
/// 开始查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e)
{
string trainTeleCode = this.trainTeleCode.Text.ToString(); string randCode = this.txtRandCode.Text.ToString(); string fromtime = this.txtDate.Text; if (string.IsNullOrEmpty(trainTeleCode) || string.IsNullOrEmpty(randCode) || string.IsNullOrEmpty(fromtime))
{
MessageBox.Show("参数不完整,请重新填写!"); return;
} DateTime fromDate = DateTime.Parse(fromtime); string queryResult = TrainTicketPriceQuery.DoGetTrainTicketLeft(trainTeleCode,fromtime, randCode); System.Windows.Documents.FlowDocument doc = rtxResultInfo.Document; doc.Blocks.Clear(); rtxResultInfo.AppendText(queryResult); } private void Button_Click_1(object sender, RoutedEventArgs e)
{
InitialRandCode();
} private void rtxResultInfo_TextChanged(object sender, TextChangedEventArgs e)
{ } private void txtRandCode_TextChanged(object sender, TextChangedEventArgs e)
{ }
}
}
这是操作