直接打包下載:http://www.sufeinet.com/thread-1843-1-1.html
讓我說實現其實趕集網的沒有什么技術含量,不過給新手們增加了學習HttpHelper的例子還是很不錯的。
下面我們一起來看看趕集網的登錄界面吧
文章中使用到的HttpHelper類大家可以直接去下載網址是:http://www.sufeinet.com/thread-3-1-1.html
部分代碼如下
private void button1_Click(object sender, 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代碼
string html = 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;
}
}
}
實現后的界面

