部分代碼如下
?
privatevoid button1_Click(objectsender, EventArgs e)
{
//參數類
item =new HttpItem()
{
URL ="http://www.ganji.com/user/login.php",//URL 必需項
Encoding ="utf-8",//編碼格式(utf-8,gb2312,gbk) 可選項 默認類會自動識別
Method ="Post",//URL 可選項 默認為Get
ContentType ="application/x-www-form-urlencoded",//返回類型 可選項有默認值
Postdata ="checkCode=&expireDays=0&next=&password="
+ URLEncode(textBox2.Text.Trim()) +
"&setcookie=0&source=passport&username="
+ URLEncode(textBox1.Text.Trim()),//Post數據 使用URLEncode是為了解決中文用戶名或者密碼的問題 可選項GET時不需要寫
};
//得到HTML代碼
stringhtml = http.GetHtml(item);
cookie = item.Cookie;
//如果cookie存在說明登錄成功
if(!string.IsNullOrEmpty(cookie))
{
//登錄成功後訪問一下<a href=\"http://www.ganji.com/vip/account/edit_userinfo.php\" target=\"_blank\">http://www.ganji.com/vip/account/edit_userinfo.php</a> 看看是不是真的登錄成功了
item =new HttpItem()
{
URL ="http://www.ganji.com/vip/index.php",//URL 必需項
Encoding ="utf-8",//編碼格式(utf-8,gb2312,gbk) 可選項 默認類會自動識別
Method ="get",//URL 可選項 默認為Get
Cookie = cookie//當前登錄Cookie
};
//得到HTML代碼
html = http.GetHtml(item);
//正則驗證余額是否存在
if(Regex.IsMatch(html, @"\d{1,10}.\d{1,2}</em>元</span>"))
{
richTextBox1.Text ="登錄成功" + html;
}
else
{
richTextBox1.Text ="登錄失敗" + html;
}
}
}
實現後的界面