当前位置:编程学习 > C#/ASP.NET >>

C#实现自动登录赶集网--HttpHelper版

部分代码如下
 
?
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;
            }
        }
    } 
 
实现后的界面
 
 
\
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,