GET request 12306 website link GET train station code information updated to the background database table

Source: Internet
Author: User

Previously, I wrote "HTTP GET request 12306 website interface to GET the station name and station Code ". The link address is:

Http://www.cnblogs.com/litao4047/archive/2013/05/31/3110781.html

The link indicates that the station name and code are obtained from the get resolution address on the 12306 website, and the data obtained is the js file on the website (the data has not been processed ), train station code dictionary: station_name.js.

Today, we want to talk about how to process the data obtained from the above link and write an insert method to update the data to the database table. The obtained data is processed as follows:

For example, a piece of data obtained isvar station_names ='@ Bjb | Beijing North | VAP | beijingbei | bjb | 0'After processing (Field Segmentation), the data is 'beijingbei' and 'vap ', and then the data is updated to the background database.

First, split the data such as '@ bjb | Beijing North | VAP | beijingbei | bjb | 0' into six fields firstLetter (initial), name (site name ), code (site code), pinyin (full spelling), shorthand (abbreviation), order (sorting), create a Model class for data storage, sample code:

Name ;{{ name = code ;{{ code = firstLetter ;{{ firstLetter = pinyin ;{{ pinyin = shorthand ;{{ shorthand = order ;{{ order =Station Site Information Model

Second, use the get request to upload the <Station> generic set and return the list.

/// <Summary> /// obtain the station information /// </summary> /// <param name = "timeout"> </param> /// <param name = "userAgent"> </param> // <param name = "cookie"> </param> public static List <Station> GetStations () {string formUrl = "http://dynamic.12306.cn/otsweb/js/common/station_name.js"; CookieContainer cookieContainer = new CookieContainer (); HttpWebRequest request = WebRequest. create (formUrl) as HttpWebRequest; reques T. method = "GET"; 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; HttpWebResponse SendSMSResponse = (HttpWebResponse) request. getResponse (); StreamReader SendSMSResponseStream = new StreamReader (SendSMSResponse. getResponseStream (); string response = SendSMSResponseStream. readToEnd (); List <Station> list = new List <Station> (); try {var str = response; Regex stationNamesRegex = new Regex ("'(? <StationNames> [^ \ '] *?) '"); If (stationNamesRegex. isMatch (str) {string stationNames = stationNamesRegex. matches (str) [0]. groups ["stationNames"]. value; string [] stations = stationNames. split ('@'); foreach (var station in stations) {if (string. isNullOrEmpty (station) {continue;} string [] names = station. split ('|'); list. add (new Station () {Shorthand = names [0], Name = names [1], Code = names [2], Pinyin = names [3], firstLetter = names [4], Order = names [5]}) ;}} catch (Exception) {} SendSMSResponse. close (); SendSMSResponseStream. close (); return list ;}

Connect to the MySql database and write an insert method to update the data obtained from the cache to the database table.

/// <Summary> /// insert data into the data_jtfw_hc_code table /// </summary> /// <param name = "t_name"> </param> /// <param name = "t_code"> </param> public static void InsertData (string t_name, string t_code) {using (MySqlConnection conn = new MySqlConnection (MySqlString) {string mysql = string. format ("insert into data_jtfw_hc_code (name, code) values ('{0}', '{1}')", t_name, t_code); MySqlCommand cmd = new MySqlCommand (mysql, conn); conn. open (); cmd. executeNonQuery (); conn. close (); conn. dispose ();}}

Finally, call the methods for obtaining site information and updating database table data to obtain processed information that already exists in the list set. instantiate the Model class and traverse the data based on the name and code fields, update to database... An example of the Code called in the Main () method is as follows:

  Main(<Station> lst =  List<Station>= (Station st 

 

Click "execute" to update all obtained information to the database easily...

The above is a complete sample code. If you want the source code, please click the following link and enter the access password (access password: 83f2 ):

Http://yunpan.cn/QegWkHcdegSF4

For reference only !!!

 

 

 

Related Article

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.