📄 frmlogin.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using feiyun0112.cnblogs.com.CSDNReader.Functions;
using feiyun0112.cnblogs.com.CSDNReader.Model;
using System.IO;
namespace feiyun0112.cnblogs.com.CSDNReader
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent();
InitData();
}
private string _viewstate;
private string __VIEWSTATE
{
get { return _viewstate; }
set { _viewstate = value; }
}
private string _UserName;
private string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
private string _Password;
private string Password
{
get { return _Password; }
set { _Password = value; }
}
private string _ClientKey;
private string ClientKey
{
get { return _ClientKey; }
set { _ClientKey = value; }
}
private string _ExPwd;
private string ExPwd
{
get { return _ExPwd; }
set { _ExPwd = value; }
}
private bool _SaveState;
private bool SaveState
{
get { return _SaveState; }
set { _SaveState = value; }
}
private string _eventvalidation;
private string __EVENTVALIDATION
{
get { return _eventvalidation; }
set { _eventvalidation = value; }
}
private void InitData()
{
DelegateFunc.OnRefreshInfo("正在读取登录信息");
this.UserName = txtUserName.Text;
this.Password = txtPassword.Text;
this.ExPwd = txtExPwd.Text;
this.SaveState = chkSaveState.Checked;
string strHTML = "";
strHTML = WEBFunc.GetSyncHTML(WEBFunc.CreateWebRequest( "http://passport.csdn.net/UserLogin.aspx?from=http%3a%2f%2fhi.csdn.net%2fmy.html"));
if (strHTML.Length!=0)
try
{
this.__VIEWSTATE =System.Web.HttpUtility.UrlEncode( RegexFunc.GetMatch(strHTML, "id=\"__VIEWSTATE\" value=\"(?<VIEWSTATE>.*?)\""));
this.ClientKey = System.Web.HttpUtility.UrlEncode(RegexFunc.GetMatch(strHTML, "name=\"ClientKey\" value=\"(?<ClientKey>.*?)\""));
this.__EVENTVALIDATION = System.Web.HttpUtility.UrlEncode(RegexFunc.GetMatch(strHTML, "id=\"__EVENTVALIDATION\" value=\"(?<EVENTVALIDATION>.*?)\""));
}
catch
{
this.__VIEWSTATE ="";
this.ClientKey ="";
this.__EVENTVALIDATION = "";
}
System.Drawing.Image img = null;
img = WEBFunc.GetSyncImg(WEBFunc.CreateWebRequest("http://passport.csdn.net/" + RegexFunc.GetMatch(strHTML, "id=\"MzImgExpPwd\" alt=\"\" src=\"(?<VIEWSTATE>.*?)\"")));
if (img!=null)
try
{
this.picExPwd.Image = img;
}
catch
{
this.picExPwd = null;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void txtUserName_TextChanged(object sender, EventArgs e)
{
this.UserName = txtUserName.Text;
}
private void txtPassword_TextChanged(object sender, EventArgs e)
{
this.Password = txtPassword.Text;
}
private void txtExPwd_TextChanged(object sender, EventArgs e)
{
this.ExPwd = txtExPwd.Text;
}
private void chkSaveState_CheckedChanged(object sender, EventArgs e)
{
this.SaveState = chkSaveState.Checked;
}
private void btnLogin_Click(object sender, EventArgs e)
{
if (Login())
{
this.Close();
return;
}
InitData();
}
private bool Login()
{
if (!ValidCheck())
{
return false;
}
DelegateFunc.OnRefreshInfo("正在登录");
string strHTML = "";
////string strPostData = String.Format("__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={0}&selLoginBy=UserName&tb_LoginName={1}&tb_LoginEMail=&tb_Password={2}&ClientKey={3}&tb_ExPwd={4}{5}&from=passport.aspx&__EVENTVALIDATION={6}&Image_Login.x=33&Image_Login.y=11"
//// ,this.__VIEWSTATE
//// ,this.UserName
//// ,this.Password
//// ,this.ClientKey
//// ,this.ExPwd
//// , (this.SaveState ? "&cb_SaveState=on" : "")
//// ,this.__EVENTVALIDATION
//// );
string strPostData = String.Format("__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={0}&ctl00%24CPH_Content%24tb_LoginNameOrLoginEmail={1}&ctl00%24CPH_Content%24tb_Password={2}&ctl00%24CPH_Content%24tb_ExPwd={3}&ClientKey={4}&ctl00%24CPH_Content%24cb_SaveState=on&from=http%3A%2F%2Fhi.csdn.net%2Fmy.html&MailParameters=&MailParameters=from%3Dhttp%253a%252f%252fhi.csdn.net%252fmy.html&ctl00%24CPH_Content%24Image_Login.x=41&ctl00%24CPH_Content%24Image_Login.y=10"
, this.__VIEWSTATE
, this.UserName
, this.Password
, this.ExPwd
,this.ClientKey
);
strHTML = WEBFunc.GetSyncHTML(WEBFunc.CreateWebRequest("http://passport.csdn.net/UserLogin.aspx?from=http%3a%2f%2fhi.csdn.net%2fmy.html",string.Empty, strPostData, System.Text.Encoding.UTF8));
string strAlert = RegexFunc.GetMatch(strHTML, "alert\\(\"(.*)\"\\)");
if (strAlert.Length == 0)
{
strAlert = RegexFunc.GetMatch(strHTML, "alert\\(\'(.*)\'\\)");
}
if( strAlert.Length!=0)
{
MsgFunc.ShowWarning(strAlert);
return false;
}
try
{
User login = new User();
login.Name = this.UserName;
login.Cookies = Variant.CurrentUser.Cookies;
//login.ReqCookies = wFunc.ReqCookies;
Variant.CurrentUser = login;
if (chkSaveState.Checked)
{
CommonFunc.SerializeObject(login, Variant.UserFileName );
}
else
{
if (File.Exists(Variant.UserFileName))
{
File.Delete(Variant.UserFileName);
}
}
}
catch(Exception ex)
{
Variant.CurrentUser.Name=string.Empty;
MsgFunc.ShowWarning(ex.Message);
return false;
}
return true;
}
private bool ValidCheck()
{
if (this.UserName .Length==0)
{
MsgFunc.ShowWarning("登录名必须填写");
return false;
}
if (this.Password .Length==0)
{
MsgFunc.ShowWarning("密码必须填写");
return false;
}
if (this.ExPwd .Length==0)
{
MsgFunc.ShowWarning("校验码必须填写");
return false;
}
return true;
}
private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
{
}
private void linkLabelReg_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
string URL;
URL = "http://passport.csdn.net/CSDNUserRegister.aspx";
System.Diagnostics.Process.Start(URL);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -