*以下為內容僅於用技術交流學習,歡迎指教.
/// <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;
}
}
