Use the http post request 12306 website interface to query the train request API

Source: Internet
Author: User

Use the interface provided by the 12306 website to pass in parameters and parse data. First, let's take a look at the link...

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

Interface Type:

HTTP (post)/UTF-8

Interface return value:

JSON

Parameters:

Method = queryststrainall
Constant, indicating the number of trains to query. (This parameter is the get parameter)

Date = 2013-1-1 1-1
Specifies the date to be queried.

Fromstation = BJP
The START Station Code. For the code dictionary of all stations, see the appendix "station_name.js ".

Tostation = Shh
Terminal code, same as above.

Starttim = 00:00 -- 24:00
Specify the travel time range.
Enumeration: 00: 00--24: 00,00: 00--06: 00,06: 00--12: 00,12: 00--18: 00,18: 00--24: 00.

HTTP defines different methods for interaction with the server. The most basic methods are get and post. Retained post is only used for site update. Post indicates a request that may change resources on the server. Each HTTP-GET and HTTP-POST consists of an HTTP request header that defines what the client requests from the server. Like a HTTP-GET, HTTP-POST parameters are URL encoded. However, the variable name/variable value is not transmitted as part of the URL, but is transmitted within the actual HTTP request message. Post is another way to retrieve data, but at the beginning of the request, the browser needs to send additional data to the server, such as cookies, usernames, passwords, and so on, the data is not reflected in the URL. After sending the data, the server sends the data (webpage) to the browser.

The http post request is used to send the URL and data. The 12306api accepts the request, parses the request, and returns the corresponding request data. For example:

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

When data is transmitted, there are four parameters: date is the date, fromstation is the origin site, tostation is the terminal, and starttime is the time. In short, the data of the Data Request returns all the train information of the departure station and the terminal station during this date period. The sample code is as follows:

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 1-1 & fromstation = vnp & tostation = Shh & starttime = 00:00 -- 24:00 "; cookiecontainer = new cookiecontainer (); // convert the submitted string data to a byte array byte [] postdata = encoding. utf8.getbytes (formdata); // you can specify the parameter httpwebrequest = 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.20.6.2152 ;. net CLR 3.5.30729) "; request. cookiecontainer = cookiecontainer; request. contentlength = postdata. length; // submit the request data 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 ();}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.