使用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();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM