*以下為內容僅於用技術交流學習,歡迎指教.
/// <summary> /// 登入 /// </summary> /// <param name="obj">此參數是為了符合WaitCallback委託的方法簽名而設,無實際意義</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(); //通過此路徑取得登入需要的loginRand變數的值,隨機碼. string uri = "https://dynamic.12306.cn/otsweb/loginAction.do?method=loginAysnSuggest"; string randStr = CommUitl.getString(uri, CommData.cookieContainer); randStr = randStr.Split(',')[0].Split(':')[1].Replace("\"", string.Empty); //登入請求提交路徑 uri = "https://dynamic.12306.cn/otsweb/loginAction.do?method=login"; // 為參數變數賦值 List<KeyValuePair<string, string>> param = new List<KeyValuePair<string, string>>(); // 隨機碼 param.Add(new KeyValuePair<string, string>("loginRand", randStr)); // 退票登入 param.Add(new KeyValuePair<string, string>("refundLogin", "N")); // 退票標識 param.Add(new KeyValuePair<string, string>("refundFlag", "Y")); // 登入使用者名稱 param.Add(new KeyValuePair<string, string>("loginUser.user_name", txtUserName.Text)); // param.Add(new KeyValuePair<string, string>("nameErrorFocus", string.Empty)); // 密碼 param.Add(new KeyValuePair<string, string>("user.password", txtPassword.Text)); // param.Add(new KeyValuePair<string, string>("passwordErrorFocus", string.Empty)); // 圖形驗證碼 param.Add(new KeyValuePair<string, string>("randCode", txtVerificationCode.Text)); // param.Add(new KeyValuePair<string, string>("randErrorFocus", string.Empty)); HttpWebResponse response = null; try { // 提交登入請求 response = HttpWebResponseUtility.CreatePostHttpResponse(uri, param, null, DefaultUserAgent, Encoding.ASCII, cookieCollection, uri); } catch (Exception ex) { // 顯示異常資訊 this.Invoke(this.showMsgDelegate, ex.Message); } if (response != null) { // 得到返回的資料流 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("系統維護中!"); this.Invoke(this.showMsgDelegate, "系統維護中!"); Thread.Sleep((Int32)numInterval.Value*1000); login(null); } 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(this.showMsgDelegate, "登入成功:用"+timeSpanStr + "/" + count + " 次"); // 觸發登入成功事件 this.Invoke(LoggedDelegate); // 修改要發布到微博的預設內容 this.Invoke(shareToWeiboDelegate, new object[] { "我用#德廣火車票助手#經過" + timeSpanStr + "嘗試登入" + count + "次後,成功登入上了12306.cn!你用了多長時間才登入成功的呢?" }); //openie(); //this.Invoke(this.showMsgDelegate, "登入成功!"); } else { Trace.WriteLine(html); login(null); } Trace.WriteLine(response.StatusCode); } else { login(null); } } catch (WebException webex) { this.Invoke(this.showMsgDelegate, webex.Message); login(null); } catch (Exception ex) { MessageBox.Show(ex.Message, "異常", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { this.Invoke(setControlTextDelegate, new object[] { btnLogin, "登入", true }); running = false; } }
原始碼請見 :https://code.google.com/p/deguang-tickets-helper