In this article, I will teach you to use the C # Simulation Web page to log on to the 12306 website, automatically swipe the ticket, select the booking person, to the final step to submit the order. Children who have studied the HTTP protocol know that when we visit the site, there are two ways, post and get, the HTTP protocol is part of TCP/IP, interested in using socket communication can be used to simulate the HTTP access mechanism. We say the post and get mode, when accessing a page, the browser will submit a local cookie submitted to the Web server, the role of cookies may be to save our landing site after the success of a series of keys, can also be some other important information. This is a crucial step. Let's get down to the chase.
Let's learn about the 12306 login method, we use HTTP tracking to find his login address.
Https://kyfw.12306.cn/otn/login/loginAysnSuggest
A form data was submitted during the login process, including Loginserdto.user_name, Userdto.password, Randcode. The first time I saw it was a bit sad, so a big website, password transmission is clear text.
The first parameter is our user name, the second is the password, and the third is the check code.
The next thing we need to do is get the login verification code. We see the address of the verification code is
This is the address of a picture, and we will point this image address to the image path of our PictureBox control. The final landing screen is like this.
Create a new HttpWebRequestExtension.cs class, add our core code, including submit order data, get Web page content, get check code picture.
///<summary>///Simulate Web page operation, submit, get order page data///</summary>PublicClasshttpwebrequestextension {PrivateStaticString ContentType ="application/x-www-form-urlencoded";PrivateStaticstring accept ="Image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/x-shockwave-flash, Application/x-silverlight, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, Application/x-ms-application, APPLICATION/X-MS-XBAP, Application/vnd.ms-xpsdocument, Application/xaml+xml, APPLICATION/X-SILVERLIGHT-2-B1, */*";PrivateStaticString useragent ="mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; trident/5.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0;. net4.0c;. net4.0e; Zune 4.7; BOIE9; ZHCN)";PrivateStaticString referer ="https://kyfw.12306.cn/";///<summary>///Submit Order Data///</summary>///<param name= "url" ></param>///<param name= "Cookies" ></param>///<param name= "param" ></param>///<returns></returns>PublicStaticString Postwebcontent (String URL, Cookiecontainer cookie,Stringparam) {Byte[] bs =Encoding.ASCII.GetBytes (param);var HttpWebRequest =(HttpWebRequest) Httpwebrequest.create (URL); Httpwebrequest.cookiecontainer =Cookies; Httpwebrequest.contenttype =ContentType; Httpwebrequest.accept =Accept Httpwebrequest.useragent =useragent; Httpwebrequest.method ="POST"; Httpwebrequest.contentlength =Bs. Length;using (Stream Reqstream =Httpwebrequest.getrequeststream ()) {Reqstream.write (BS,0Bs. Length); }var HttpWebResponse =(HttpWebResponse) httpwebrequest.getresponse (); Stream Responsestream =Httpwebresponse.getresponsestream (); StreamReader StreamReader =NewStreamReader (Responsestream, Encoding.UTF8);String html =Streamreader.readtoend (); Streamreader.close (); Responsestream.close (); Httpwebrequest.abort (); Httpwebresponse.close ();Returnhtml }///<summary>///Get page data///</summary>///<param name= "url" ></param>///<param name= "Cookies" ></param>///<returns></returns>PublicStaticString Getwebcontent (StringURL, Cookiecontainer cookie) {var HttpWebRequest =(HttpWebRequest) Httpwebrequest.create (URL); Httpwebrequest.cookiecontainer =Cookies; Httpwebrequest.contenttype =ContentType; Httpwebrequest.referer =Referer Httpwebrequest.accept =Accept Httpwebrequest.useragent =useragent; Httpwebrequest.method ="GET"; HttpWebRequest.ServicePoint.ConnectionLimit =Int. MaxValue;var HttpWebResponse =(HttpWebResponse) httpwebrequest.getresponse (); Stream Responsestream =Httpwebresponse.getresponsestream (); StreamReader StreamReader =NewStreamReader (Responsestream, Encoding.UTF8);String html =Streamreader.readtoend (); Streamreader.close (); Responsestream.close (); Httpwebrequest.abort (); Httpwebresponse.close ();Returnhtml }///<summary>///Get the web Captcha picture///</summary>///<param name= "url" ></param>///<param name= "Cookies" ></param>///<returns></returns>PublicStaticObject Getwebimage (StringURL, Cookiecontainer cookie) {HttpWebRequest request =(HttpWebRequest) WebRequest.Create (URL); Request. Referer =Referer Request. UserAgent ="mozilla/5.0 (Windows NT 5.1) applewebkit/537.36 (khtml, like Gecko) chrome/31.0.1650.63 Safari/537.36 "; Request. Accept = "image/webp,*/*;q=0.8 "; Request. Cookiecontainer = cookie; request. ContentType = ContentType; request. KeepAlive = truetrue// request. Proxy = null; return request. GetResponse (). GetResponseStream (); } }
Then we can simulate landing 12306.
var loginres = httpwebrequestextension.postwebcontent (trainurlconstant.asynsugguesturl, CookieContainer, "loginuserdto.user_name="&&userdto.password="&&randcode=" + Userlogin.verifycode);
The result of the login is returned in JSON data format. If you see a logincheck\ ": \" Y\ ", then congratulations, you have landed on the Internet station.
If you fail, the result of the return can be seen as the cause of the login failure, message:["..."], ... Indicates the cause of the error returned, which is not listed here.
Note: The status of the cookie is saved after successful login, which is the most important link.
Then get station information. Station information stored in a JS inside, we need to decode JS.
Https://kyfw.12306.cn/otn/resources/js/framework/station_name.js
We use the browser to open a look, it is not a well-defined in the | Separated station information, we only need to extract the station name and station code. Here's how I decode it.
View Code
Just a second. To get the ticket holder information, our contact URL is https://kyfw.12306.cn/otn/confirmPassenger/getPassengerDTOs, which is a JSON data.
The JSON data contains information about the contact, including telephone, ID number, date of birth, student, gender, etc. With these basic data we can swipe tickets and buy tickets. Let's take a look at the number of votes left.
Https://kyfw.12306.cn/otn/leftticket/query?leftticketdto.train_date={0}&leftticketdto.from_station={1} &leftticketdto.to_station={2}&purpose_codes=adult
The data inside the URL is sent from the Origin station, terminal, departure date and other information. We use get method to get the remainder of the ticket information
From, to); var trainleftticketinfores = httpwebrequestextension.getwebcontent (URL, cookiecontainer);
The returned JSON data contains the flag of the ticket, that is, the SECRETSTR, as well as the departure time, soft berth number, hard sleeper number, soft seat, hard seat number and so on.
With this data we can choose to automatically swipe the ticket.
Next choose the seat, the train began to rob tickets.
Select the train to confirm the submission of our selected train information, we look at the parameters he needs to pass information
View Code
12306 submitting an order using a random token information for an order, we must first obtain token information
So where do the tokens, Key_check_ischange, LEFTTICKETSTR, and train_location come from in this form? This is the time to test patience, after careful search found that the original information is hidden in the Web page JS inside. Page address is https://kyfw.12306.cn/otn/confirmPassenger/initDc not careful to see it really does not come out AH.
"_json_att="
The other two parameters passengerticketstr, Oldpassengerstr is we select the ticket buyers, carefully analyze the string, found that there is a regular, each ticket buyers are separated by _. Comma before the first data represents the seat number, the comma of the fourth data is a contact, remember the need to use a URL encoding format, the 6th is a social Security number, 7th is a mobile phone number.
Then get the check code before submitting the order HTTPS://KYFW.12306.CN/OTN/PASSCODENEW/GETPASSCODENEW?MODULE=PASSENGER&RAND=RANDP
Pbxordercode.image =
Submit the form information we found above to the site check is not a problem
View Code
The result of the correct order return
If none of the above is a problem, then you can move on to the real meaning of the ticket-snatching process. Let's take a look at the URL of the ticket.
Https://kyfw.12306.cn/otn/confirmPassenger/confirmSingleForQueue
View Code
The last step is a bad one. Let's see if the order number is generated.
View Code
Look at the JSON results returned there is no OrderID, when the OrderID is greater than 0, indicating that your ticket has been robbed. Quickly log on to the website to pay for it.