/// <summary> /// 得到验证码 内部重复调用 直到得到验证码 /// </summary> private void getVerificationCode() { sleep(); count++; string url = "https://dynamic.12306.cn/otsweb/passCodeAction.do?rand=lrand"; HttpWebRequest request2 = HttpWebResponseUtility.CreateGetHttpResponse(url, cookieContainer, "https://dynamic.12306.cn/otsweb/loginAction.do?method=login"); HttpWebResponse response = null; try { response = (HttpWebResponse)request2.GetResponse(); } catch (Exception ex) { //MessageBox.Show("连接12306.cn网站出错!","异常", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); this.Invoke(this.showMsgDelegate, "取得验证码失败:" + ex.Message); getVerificationCode(); } if (response != null) { Stream responseStream = response.GetResponseStream(); response.Cookies = request2.CookieContainer.GetCookies(new Uri(url)); cookieCollection = response.Cookies; if (string.IsNullOrEmpty(cookieStr) == true) { cookieStr = response.Headers.Get("Set-Cookie"); } cookieContainer.SetCookies(new Uri(url), cookieStr); Image original = Image.FromStream(responseStream); picValidImg.Image = original; this.Invoke(this.showMsgDelegate, "取得验证码成功!"); this.Invoke(setControlTextDelegate, new object[] { btnLogin, "登录", true }); endTime = DateTime.Now; timeSpan = endTime.Subtract(beginTime); if (count > 1) { MessageBox.Show("12306不给力啊!!!尝试了" + count + "次用了" + getTimeSpanString(timeSpan) + "后才得到验证码图片.", "德广火车票助手 温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Invoke(activateDelegate); this.Invoke(shareToWeiboDelegate, new object[] { "12306不给力啊,登录页面尝试了" + count + "次用了" + getTimeSpanString(timeSpan) + "后,才显示出来.还好有#德广火车票助手#帮助,不然就鼠标键盘就被我按报废啦!" }); } } }
/// <summary> /// 登录 /// </summary> /// <param name="obj"></param> private void login(object obj) { try { running = true; sleep(); if (stop == true) { return; } if (obj != null) { Thread.CurrentThread.Name = obj.ToString(); } if (logged == true) { if (MessageBox.Show("您已经登录,您需要再次进入12306网站吗?需要您已经退出,就需要重新登录!", "德广火车票助手 温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { this.Invoke(LoggedDelegate); //openie(); } running = false; return; } Trace.WriteLine("login()"); count++; System.Net.ServicePointManager.CertificatePolicy = new MyPolicy(); // this is what we are sending string post_data = "loginUser.user_name=tony12306cn&nameErrorFocus=&user.password=tony1234&passwordErrorFocus=&randCode=" + txtVerificationCode.Text + "&randErrorFocus=focus"; // this is where we will send it string uri = "https://dynamic.12306.cn/otsweb/loginAction.do?method=login"; // create a request //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); //request.CookieContainer = cookieContainer; //request.KeepAlive = false; //request.ProtocolVersion = HttpVersion.Version10; //request.Method = "POST"; //// turn our request string into a byte stream //byte[] postBytes = Encoding.ASCII.GetBytes(post_data); //// this is important - make sure you specify type this way //request.ContentType = "application/x-www-form-urlencoded"; //request.ContentLength = postBytes.Length; //Stream requestStream = request.GetRequestStream(); //// now send it //requestStream.Write(postBytes, 0, postBytes.Length); //requestStream.Close(); Dictionary <string, string> param = new Dictionary <string, string>(); param.Add("loginUser.user_name", txtUserName.Text); param.Add("nameErrorFocus", string.Empty); param.Add("user.password", txtPassword.Text); param.Add("passwordErrorFocus", string.Empty); param.Add("randCode", txtVerificationCode.Text); param.Add("randErrorFocus", "focus"); HttpWebResponse response = null; try { response = HttpWebResponseUtility.CreatePostHttpResponse(uri, param, null, DefaultUserAgent, Encoding.ASCII, cookieCollection, uri); } catch (Exception ex) { this.Invoke(this.showMsgDelegate, ex.Message); //showInfo(ex.Message); } if (response != null) { // grab te response and print it out to the console along with the status code //HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream receiveStream = response.GetResponseStream(); if (response.ContentEncoding.ToLower().Contains("gzip")) { receiveStream = new GZipStream(receiveStream, CompressionMode.Decompress); } html = new StreamReader(receiveStream).ReadToEnd(); if (html.IndexOf("当前访问用户过多") > 0) { this.Invoke(this.showMsgDelegate, "当前访问用户过多"); login(null); } else if (html.IndexOf("请输入正确的验证码") > 0) { messageBoxShowInfo("请输入正确的验证码!"); this.Invoke(focusDelegate, new object[] { txtVerificationCode }); this.Invoke(setControlTextDelegate, new object[] { txtVerificationCode, string.Empty, true }); getVerificationCode(this); } else if (html.IndexOf("登录名不存在") > 0) { messageBoxShowInfo("登录名不存在!!"); this.Invoke(focusDelegate, new object[] { txtUserName }); } else if (html.IndexOf("密码输入错误") > 0) { messageBoxShowInfo("密码输入错误,如果多次输入错误可能会被锁定帐户!"); this.Invoke(focusDelegate, new object[] { txtPassword }); this.Invoke(setControlTextDelegate, new object[] { txtPassword, string.Empty, true }); } else if (html.IndexOf("已经被锁定") > 0) { messageBoxShowInfo("您的用户已经被锁定,请稍候再试!"); } else if (html.IndexOf("系统维护中") > 0) { messageBoxShowInfo("系统维护中!"); } else if (html.IndexOf("我的12306") > 0) { this.Invoke(activateDelegate); endTime = DateTime.Now; logged = true; timeSpan = endTime.Subtract(beginTime); timeSpanStr = getTimeSpanString(timeSpan); MessageBox.Show("经过 " + timeSpanStr + ", " + count + " 次的尝试后,您已经登录成功!" + Environment.NewLine + "点击确定打开12306网站,请忽略登录界面,直接点击\"车票预订\"就可以啦!" + Environment.NewLine + Environment.NewLine + "深圳市德广信息技术有限公司 祝您:" + Environment.NewLine + "回家一路顺风!全家身体健康!幸福快乐!事事如意!", "德广火车票助手 恭喜您", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1); this.Invoke(shareToWeiboDelegate, new object[] { "我用#德广火车票助手#经过" + timeSpanStr + "尝试登录" + count + "次后,成功登录上了12306.cn!你用了多长时间才登录成功的呢?" }); this.Invoke(LoggedDelegate); //openie(); this.Invoke(this.showMsgDelegate, "登录成功!"); } else { Trace.WriteLine(html); login(null); } Trace.WriteLine(response.StatusCode); } else { login(null); } } catch (Exception ex) { MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Invoke(setControlTextDelegate, new object[] { btnLogin, "登录", true }); running = false; } }