使用HTTP POST请求12306网站接口查询火车车次API

使用12306网站提供的接口,传入参数,解析数据,先发个链接给大家看看...

   http://www.devapi.org/12306-Search-Train-Number.html

接口类型:

   HTTP(POST) / UTF-8

接口返回值:

   json

参数详解:

   method=queryststrainall
   常量,表示车次查询。(此参数为get参数)

   date=2013-1-1
   指定要查询的日期。

   fromstation=BJP
   起始站代号,所有车站的代号字典,见文后附件“station_name.js”。

   tostation=SHH
   终点站代号,同上。

   starttim=00:00--24:00
   指定乘车时间区间。
   枚举:00:00--24:00,00:00--06:00,06:00--12:00,12:00--18:00,18:00--24:00。

HTTP 定义了与服务器交互的不同方法,最基本的方法是 GET 和 POST。保留 POST 仅用于更新站点。POST 表示可能改变服务器上的资源的请求。每一个HTTP-GET和HTTP-POST都由HTTP请求头组成,这些请求头定义了客户端从服务器请求了什么。与HTTP-GET类似,HTTP-POST参数也是被URL编码的。然而,变量名/变量值不作为URL的一部分被传送,而是放在实际的HTTP请求消息内部被传送。POST是另一种取数据的方式,但是在请求的一开始浏览器要向服务器额外发送一些数据,例如cookie、用户名、密码等等,这些数据不体现在URL上,发送完毕后服务器再将数据(网页)传送给浏览器。

利用HTTP POST请求,发送URL和DATA,12306API接口接受请求,解析请求,返回相应的请求数据。如:

   URL:http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryststrainall

   Data:date=2013-1-1&fromstation=BJP&tostation=SHH&starttime=00:00--24:00

   CoontentType:application/x-www-form-urlencoded

传Data的时候有四个参数date是日期,fromstation是始发站,tostation是终点站,starttime是时间。总之,Data请求的数据,返回始发站和终点站在这个日期时间段内所有的列车班次信息。示例代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml;
using System.IO;

namespace Address_Resolution
{
    class Program
    {
        static void Main(string[] args)
        {
            string formUrl ="http://dynamic.12306.cn/otsquery/query/queryRemanentTicketAction.do?method=queryststrainall";

            string formData = "date=2013-1-1&fromstation=VNP&tostation=SHH&starttime=00:00--24:00";
            
            CookieContainer cookieContainer = new CookieContainer();
            // 将提交的字符串数据转换成字节数组  
            byte[] postData = Encoding.UTF8.GetBytes(formData);
            // 设置提交的相关参数  
            HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
            Encoding myEncoding = Encoding.GetEncoding("gb2312");
            request.Method = "POST";
            request.KeepAlive = false;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/x-www-form-urlencoded"; 
           request.UserAgent=
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.CookieContainer = cookieContainer; 
            request.ContentLength = postData.Length;
            // 提交请求数据  
            System.IO.Stream outputStream = request.GetRequestStream();
            outputStream.Write(postData, 0, postData.Length); 
            outputStream.Close();
            HttpWebResponse response; 
            Stream responseStream;
            StreamReader reader; 
            string srcString;
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
            srcString = reader.ReadToEnd();
            reader.Close();
            Console.WriteLine(srcString);
            Console.Read();
        }
    }
}

 

posted on 2013-05-31 17:02  行意  阅读(4510)  评论(3编辑  收藏  举报

导航