Exemplo n.º 1
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <returns>返回请求结果JSON字符串,AccessToken需自行根据不同应用进行提取</returns>
        public virtual string GetAccessToken(Dictionary<string, string> dic, HttpMethod.RequestMethod method)
        {
            UriBuilder uri = new UriBuilder(AccessTokenRequestUrl);
            string queryString = Common.BuildQueryString(dic);
            uri.Query = queryString;
            string result = string.Empty;

            if (method == HttpMethod.RequestMethod.GET) //GET请求
            {
                HttpGet get = new HttpGet(uri.ToString());
                result = get.SendReqReturnResponseString();
            }
            else if (method == HttpMethod.RequestMethod.POST) //POST请求
            {
                HttpPost post = new HttpPost(AccessTokenRequestUrl, queryString);
                result = post.SendReqReturnResponseString();
            }
            return result;
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取AccessToken
        /// </summary>
        /// <returns>返回请求结果JSON字符串,AccessToken需自行根据不同应用进行提取</returns>
        public virtual string GetAccessToken(Dictionary <string, string> dic, HttpMethod.RequestMethod method)
        {
            UriBuilder uri         = new UriBuilder(AccessTokenRequestUrl);
            string     queryString = Common.BuildQueryString(dic);

            uri.Query = queryString;
            string result = string.Empty;

            if (method == HttpMethod.RequestMethod.GET) //GET请求
            {
                HttpGet get = new HttpGet(uri.ToString());
                result = get.SendReqReturnResponseString();
            }
            else if (method == HttpMethod.RequestMethod.POST) //POST请求
            {
                HttpPost post = new HttpPost(AccessTokenRequestUrl, queryString);
                result = post.SendReqReturnResponseString();
            }
            return(result);
        }
Exemplo n.º 3
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Dictionary<string, string> postdic = new Dictionary<string, string>();
            List<string> keys = new List<string>();
            List<string> values = new List<string>();
            SendOrderReqParams postdic = new SendOrderReqParams(keys, values);
            postdic.Add("org.apache.struts.taglib.html.TOKEN", token);
            postdic.Add("leftTicketStr", leftTicketStr);
            postdic.Add("textfield", "中文或拼音首字母");//搜索乘客框value
            for (int j = 0; j < this.checkedListBox1.Items.Count; j++)
            {
                if (checkedListBox1.GetItemChecked(j))
                {
                    postdic.Add("checkbox" + j.ToString(), j.ToString());
                }
            }
            postdic.Add("orderRequest.train_date", queryTicketParams["orderRequest.train_date"]);
            postdic.Add("orderRequest.train_no", orderParams["trainno4"]);
            postdic.Add("orderRequest.station_train_code", orderParams["station_train_code"]);
            postdic.Add("orderRequest.from_station_telecode", queryTicketParams["orderRequest.from_station_telecode"]);
            postdic.Add("orderRequest.to_station_telecode", queryTicketParams["orderRequest.to_station_telecode"]);
            postdic.Add("orderRequest.seat_type_code", string.Empty);
            postdic.Add("orderRequest.ticket_type_order_num", string.Empty);
            postdic.Add("orderRequest.bed_level_order_num", "000000000000000000000000000000");
            postdic.Add("orderRequest.start_time", orderParams["train_start_time"]);
            postdic.Add("orderRequest.end_time", orderParams["arrive_time"]);
            postdic.Add("orderRequest.from_station_name", orderParams["from_station_name"]);
            postdic.Add("orderRequest.to_station_name", orderParams["to_station_name"]);
            postdic.Add("orderRequest.cancel_flag", "1");
            postdic.Add("orderRequest.id_mode", "Y");

            int passengerCounts = 1;
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                if (i == this.dataGridView1.Rows.Count - 1)
                {
                    continue;
                }
                Dictionary<string, string> passengetDic = new Dictionary<string, string>();
                passengetDic.Add(string.Format("passenger_{0}_seat", passengerCounts), "M");
                passengetDic.Add(string.Format("passenger_{0}_ticket", passengerCounts), "1");
                passengetDic.Add(string.Format("passenger_{0}_name", passengerCounts), this.dataGridView1.Rows[i].Cells[3].Value.ToString().Trim());
                foreach (string key in cardTypeDic.Keys)
                {
                    if (cardTypeDic[key] == this.dataGridView1.Rows[i].Cells[4].Value.ToString())
                    {
                        passengetDic.Add(string.Format("passenger_{0}_cardtype", passengerCounts), key);
                    }
                }
                passengetDic.Add(string.Format("passenger_{0}_cardno", passengerCounts), this.dataGridView1.Rows[i].Cells[5].Value.ToString().Trim());
                passengetDic.Add(string.Format("passenger_{0}_mobileno", passengerCounts), this.dataGridView1.Rows[i].Cells[6].Value.ToString().Trim());

                string passengerTickets = string.Format("M,0,1,{0},{1},{2},{3},Y", passengetDic[string.Format("passenger_{0}_name", passengerCounts)]
                    , passengetDic[string.Format("passenger_{0}_cardtype", passengerCounts)],
                     passengetDic[string.Format("passenger_{0}_cardno", passengerCounts)],
                     passengetDic[string.Format("passenger_{0}_mobileno", passengerCounts)]);

                string oldPassengers = passengetDic[string.Format("passenger_{0}_name", passengerCounts)] + "," +
                    passengetDic[string.Format("passenger_{0}_cardtype", passengerCounts)] + "," +
                     passengetDic[string.Format("passenger_{0}_cardno", passengerCounts)];

                postdic.Add("passengerTickets", passengerTickets);
                postdic.Add("oldPassengers", oldPassengers);

                foreach (string key in passengetDic.Keys)
                {
                    postdic.Add(key, passengetDic[key]);
                }
                postdic.Add("checkbox9", "Y");
                passengerCounts++;
            }

            for (int k = 0; k < 5 - passengerCounts + 1; k++)
            {
                postdic.Add("oldPassengers", string.Empty);
                postdic.Add("checkbox9", "Y");
            }

            postdic.Add("randCode", this.textBox1.Text.Trim());
            postdic.Add("orderRequest.reserve_flag", "A");
            postdic.Add("tFlag", "dc");

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < keys.Count; i++)
            {
                sb.AppendFormat("{0}={1}&", keys[i], values[i]);
            }
            sb.Remove(sb.ToString().LastIndexOf("&"), 1);
            HttpPost post = new HttpPost("https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=checkOrderInfo&rand=" + this.textBox1.Text + "",
                sb.ToString());
            post.req.CookieContainer.Add(reqCookies);
            string resultstr = post.SendReqReturnResponseString();

            if (!string.IsNullOrEmpty(resultstr))
            {
                JObject json1 = JObject.Parse(resultstr);
                if (json1["msg"] != null && !string.IsNullOrEmpty(json1["msg"].Value<string>()))
                {
                    MessageBox.Show(json1["msg"].Value<string>());
                    return;
                }
            }
            HttpGet ticketLeftGet = new HttpGet(string.Format("https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=getQueueCount&train_date={0}&train_no={1}&station={2}&seat={3}&from={4}&to={5}&ticket={6}",
               queryTicketParams["orderRequest.train_date"], orderParams["trainno4"],
               orderParams["station_train_code"], "M", queryTicketParams["orderRequest.from_station_telecode"],
               queryTicketParams["orderRequest.to_station_telecode"],
               leftTicketStr));
            ticketLeftGet.req.CookieContainer.Add(reqCookies);
            string ticketLeftGetResult = ticketLeftGet.SendReqReturnResponseString();

            HttpPost orderpost = new HttpPost("https://dynamic.12306.cn/otsweb/order/confirmPassengerAction.do?method=confirmSingleForQueue",
                sb.ToString().Replace("&tFlag=dc", string.Empty));
            orderpost.req.CookieContainer.Add(reqCookies);
            string orderpostresult = orderpost.SendReqReturnResponseString();
        }
Exemplo n.º 4
0
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns["orderbutton"].Index == e.ColumnIndex)
            {
                string orderkey = this.dataGridView1["ordertiket", e.RowIndex].Value.ToString();
                string para1 = "getSelected('";
                orderkey = orderkey.Substring(orderkey.IndexOf(para1) + para1.Length);
                orderkey = orderkey.Substring(0, orderkey.IndexOf("')"));
                string[] data = orderkey.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
                Dictionary<string, string> postdatadic = new Dictionary<string, string>();
                postdatadic.Add("station_train_code", data[0]);
                postdatadic.Add("train_date", this.dateTimePicker1.Value.ToString("yyyy-MM-dd"));
                postdatadic.Add("seattype_num", "");
                postdatadic.Add("from_station_telecode", data[4]);
                postdatadic.Add("to_station_telecode", data[5]);
                postdatadic.Add("include_student", "00");
                postdatadic.Add("from_station_telecode_name", this.textBox1.Text);
                postdatadic.Add("to_station_telecode_name", this.textBox2.Text);
                postdatadic.Add("round_train_date", "");
                postdatadic.Add("round_start_time_str", "00:00--24:00");
                postdatadic.Add("single_round_type", "1");
                postdatadic.Add("train_pass_type", "QB");
                postdatadic.Add("train_class_arr", "QB#D#Z#T#K#QT# ");
                postdatadic.Add("start_time_str", "00:00--24:00");
                postdatadic.Add("lishi", data[1]);
                postdatadic.Add("train_start_time", data[2]);
                postdatadic.Add("trainno4", data[3]);
                postdatadic.Add("arrive_time", data[6]);
                postdatadic.Add("from_station_name", data[7]);
                postdatadic.Add("to_station_name", data[8]);
                postdatadic.Add("from_station_no", data[9]);
                postdatadic.Add("to_station_no", data[10]);
                postdatadic.Add("ypInfoDetail", data[11]);
                postdatadic.Add("mmStr", data[12]);
                postdatadic.Add("locationCode", data[13]);
                postdatadic.Add(dynamicKey, dynamicValue);
                postdatadic.Add("myversion", "undefined");

                HttpPost post = new HttpPost("https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=submutOrderRequest",
                    postdatadic);
                post.req.CookieContainer.Add(reqCookies);
                string resulthtml = post.SendReqReturnResponseString();

                //获取票种类和座位种类
                string para3 = "var limitBuySeatTicketDTO =";
                string para4 = "var ticketTypeReserveFlagMap";
                string seatAndTicketType = resulthtml.Substring(resulthtml.IndexOf(para3) + para3.Length);
                seatAndTicketType = seatAndTicketType.Substring(0,seatAndTicketType.IndexOf(para4));
                seatAndTicketType = seatAndTicketType.Substring(0, seatAndTicketType.LastIndexOf(";"));

                //string seatAndTicketType = "{\"seat_type_codes\":[{\"end_station_name\":\"\",\"end_time\":\"\",\"id\":\"1\",\"start_station_name\":\"\",\"start_time\":\"\",\"value\":\"硬座\"}],\"ticket_seat_codeMap\":{\"3\":[{\"end_station_name\":\"\",\"end_time\":\"\",\"id\":\"1\",\"start_station_name\":\"\",\"start_time\":\"\",\"value\":\"硬座\"}],\"2\":[{\"end_station_name\":\"\",\"end_time\":\"\",\"id\":\"1\",\"start_station_name\":\"\",\"start_time\":\"\",\"value\":\"硬座\"}],\"1\":[{\"end_station_name\":\"\",\"end_time\":\"\",\"id\":\"1\",\"start_station_name\":\"\",\"start_time\":\"\",\"value\":\"硬座\"}],\"4\":[{\"end_station_name\":\"\",\"end_time\":\"\",\"id\":\"1\",\"start_station_name\":\"\",\"start_time\":\"\",\"value\":\"硬座\"}]},\"ticket_type_codes\":[{\"end_station_name\":\"\",\"end_time\":\"\",\"id\":\"1\",\"start_station_name\":\"\",\"start_time\":\"\",\"value\":\"成人票\"},{\"end_station_name\":\"\",\"end_time\":\"\",\"id\":\"2\",\"start_station_name\":\"\",\"start_time\":\"\",\"value\":\"儿童票\"},{\"end_station_name\":\"\",\"end_time\":\"\",\"id\":\"3\",\"start_station_name\":\"\",\"start_time\":\"\",\"value\":\"学生票\"},{\"end_station_name\":\"\",\"end_time\":\"\",\"id\":\"4\",\"start_station_name\":\"\",\"start_time\":\"\",\"value\":\"残军票\"}]}";

                HAP.HtmlDocument doc = new HAP.HtmlDocument();
                string orderPagePath = Application.StartupPath + "\\" + Guid.NewGuid().ToString() + ".html";
                StreamWriter sw = new StreamWriter(orderPagePath);
                sw.Write(resulthtml);
                sw.Close();
                StreamReader sr = new StreamReader(orderPagePath);
                doc.Load(sr.BaseStream,Encoding.UTF8);
                sr.Close();

                HAP.HtmlNode tableTicketNode = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/div[1]/table[1]/tr[2]");
                HAP.HtmlNode tableTrainNode = doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/div[1]/table[1]/tr[1]");

                string ticketInfo = tableTicketNode.InnerText.Replace("\n", string.Empty).Replace("\t", string.Empty).Replace("\r", string.Empty).Trim();
                string trainInfo = tableTrainNode.InnerText.Replace("\n", string.Empty).Replace("\t", string.Empty).Replace("\r", string.Empty).Trim();
                string leftTicketStr = doc.GetElementbyId("left_ticket").GetAttributeValue("value", string.Empty);
                string param5 = "name=\"org.apache.struts.taglib.html.TOKEN\" value=\"";
                string token = doc.DocumentNode.InnerHtml.Substring(doc.DocumentNode.InnerHtml.IndexOf(param5) + param5.Length);
                token = token.Substring(0, token.IndexOf("\">"));

                //验证码
                string randCodeSrc = doc.GetElementbyId("img_rrand_code").GetAttributeValue("src", string.Empty);
                HttpGet randCodeGet = new HttpGet("https://dynamic.12306.cn" + randCodeSrc);
                randCodeGet.req.CookieContainer.Add(reqCookies);
                var imgName = Guid.NewGuid();
                var response = randCodeGet.SendRequest();
                string imgPath = Common.GetImageFromStream(response, Common.GetVirtualPath("\\ValidateCode\\" + imgName));
                ///otsweb/passCodeNewAction.do?module=passenger&rand=randp

                confirmorder confirmorder = new confirmorder();
                confirmorder.validateCodePath = imgPath;
                confirmorder.queryTicketParams = this.queryTicketDic;
                confirmorder.seatAndTicketType = seatAndTicketType;
                confirmorder.reqCookies = this.reqCookies;
                confirmorder.TicketInfo = ticketInfo;
                confirmorder.TrainInfo = trainInfo;
                confirmorder.orderParams = postdatadic;
                confirmorder.leftTicketStr = leftTicketStr;
                confirmorder.token = token;
                confirmorder.Show();
            }
        }
Exemplo n.º 5
0
        private void button1_Click(object sender, EventArgs e)
        {
            string validateCode = this.textBox1.Text;
            string urlPrefix = "https://dynamic.12306.cn";

            //获取JS文件
            HttpGet get1 = new HttpGet("https://dynamic.12306.cn/otsweb/loginAction.do?method=init");
            get1.req.CookieContainer.Add(reqCookies);
            string html = get1.SendReqReturnResponseString();
            string jsURL = html.Substring(html.IndexOf("/otsweb/dynamicJsAction.do?jsversion="));
            jsURL = urlPrefix + jsURL.Substring(0, jsURL.IndexOf("method=loginJs") + "method=loginJs".Length);

            //获取JS文件中的 Key
            HttpGet get2 = new HttpGet(jsURL);
            get2.req.CookieContainer.Add(reqCookies);
            string jsContent = get2.SendReqReturnResponseString();
            string dynamicKey = jsContent.Substring(jsContent.IndexOf("function gc()"));
            int beginIndex = dynamicKey.IndexOf("key='") + "key='".Length;
            dynamicKey = dynamicKey.Substring(beginIndex, dynamicKey.IndexOf("';") - beginIndex);

            //调用加密JS方法  得到加密的value
            string encryptValue = this.webBrowser1.Document.InvokeScript("finalEncrypt", new object[] { dynamicKey, "1111" }).ToString();

            //得到loginRand,refundfLag,
            HttpGet get3 = new HttpGet("https://dynamic.12306.cn/otsweb/loginAction.do?method=loginAysnSuggest");
            get3.req.CookieContainer.Add(reqCookies);
            string loginParam = get3.SendReqReturnResponseString();
            string loginRand = loginParam.Substring(loginParam.IndexOf(":\"") + 2);
            loginRand = loginRand.Substring(0, loginRand.IndexOf("\","));
            string tempStr = "randError\":\"";
            string randError = loginParam.Substring(loginParam.IndexOf(tempStr) + tempStr.Length, 1);

            Dictionary<string, string> loginDic = new Dictionary<string, string>();
            loginDic.Add("loginRand", loginRand);
            loginDic.Add("refundLogin", "N");
            loginDic.Add("refundFlag", randError);
            loginDic.Add("isClick", "");
            loginDic.Add("form_tk", "null");
            loginDic.Add("loginUser.user_name", this.textBox2.Text);
            loginDic.Add("nameErrorFocus", "");
            loginDic.Add("user.password", this.textBox3.Text);
            loginDic.Add("passwordErrorFocus", "");
            loginDic.Add("randCode", validateCode);
            loginDic.Add("randErrorFocus", "");
            loginDic.Add(HttpUtility.UrlEncode(dynamicKey), HttpUtility.UrlEncode(encryptValue));
            loginDic.Add("myversion", "undefined");

            string postdata = string.Empty;
            List<string> strs = new List<string>();
            foreach (string item in loginDic.Keys)
            {
                strs.Add(string.Format("{0}={1}", item, loginDic[item]));
            }
            postdata = string.Join("&", strs);
            HttpPost post = new HttpPost("https://dynamic.12306.cn/otsweb/loginAction.do?method=login", postdata);
            post.req.Referer = "https://dynamic.12306.cn/otsweb/loginAction.do?method=init";
            post.req.CookieContainer.Add(reqCookies);

            string loginResult = post.SendReqReturnResponseString();

            string[] error = new string[] { "密码输入错误,您还有3次机会!", "请输入正确的验证码!", "登录名不存在!" };

            if (!loginResult.Contains("欢迎您登录中国铁路客户服务中心网站"))
            {
                for (int i = 0; i < error.Length; i++)
                {
                    if (loginResult.Contains(error[i]))
                    {
                        GenerateValidateCode();
                        MessageBox.Show(error[i]);
                    }
                }
            }
            else
            {
                order order = new Winform.order();
                order.reqCookies = reqCookies;
                order.dynamicKey = dynamicKey;
                order.dynamicValue = encryptValue;
                order.Show();
            }
        }