http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 10973|回复: 3

[HttpHelper] 这个类无视cookie到底是什么概念呢

[复制链接]
发表于 2014-7-28 18:07:16 | 显示全部楼层 |阅读模式
5金钱
我看过论坛的教程,例子,里面比较少的有对cookie多次进行的操作
不知道我用这个Httphelper对cookie的操作方法是错误了是吗
我是定义了一个全局的CookieCollection HttpCookie保存请求的所有CookieCollection
调式运行发现 该类对cookie中带有逗号好像舍去了,导致了post结果不对
QQ截图20140728180052.jpg

QQ截图201407281804113.jpg
逗号后面的没了!!
也考虑用string cookie 但是连登录都没登录上去。。。

以下是模拟登录QQ邮箱的

[C#] 纯文本查看 复制代码
 class QQHelper2
    {
        HttpHelper _HttpHelper;
        public QQHelper2()
        {
            _HttpHelper = new HttpHelper();
            HttpCookie = new CookieCollection();
        }
        public static string GetRandrom(int number)
        {
            Random rand = new Random();
            string strNum = "";
            int i = 0;
            while (i < number)
            {
                i++;
                int randomNum = rand.Next(0, 10);
                strNum += randomNum.ToString();
            }
            strNum = "0." + strNum;
            return strNum;
        }
        public static string GetBetween(string s, string s1, string s2)
        {
            int n1, n2;
            n1 = s.IndexOf(s1, 0) + s1.Length;   //开始位置
            n2 = s.IndexOf(s2, n1);               //结束位置
            return s.Substring(n1, n2 - n1);   //取搜索的条数,用结束的位置-开始的位置,并返回
        }

        private string QQNumber;
        public string _QQNumber
        {
            get { return QQNumber; }
            set { QQNumber = value; }
        }
        private string QQPassword;
        public string _QQPassword
        {
            get { return QQPassword; }
            set { QQPassword = value; }
        }
        private string VeryCode;
        public string _VeryCode
        {
            get { return VeryCode; }
            set { VeryCode = value; }
        }

        private string ErrMessage;
        public string _ErrMessage
        {
            get { return ErrMessage; }
            set { ErrMessage = value; }
        }
        #region 登录变量
        private HttpItem _HttpItem;
        private HttpResult _HttpResult;
        private string login_sig;
        private string Ptuin;
        private string sid;
        private CookieCollection HttpCookie;
        #endregion
        /// <summary>
        /// 判断是否需要验证码0表示不需要,-1表示需要,-2表示出错误
        /// </summary>
        /// <returns>0表示不需要,-1表示需要,-2表示出错误</returns>
        /// 
        public int CheckVerycode()
        {
            //try
            //{
                #region 获取login_sig,Cookie
                _HttpItem = new HttpItem
                {
                    Allowautoredirect = false,
                    URL = "https://xui.ptlogin2.qq.com/cgi-bin/xlogin?appid=522005705&daid=4&s_url=https://mail.qq.com/cgi-bin/login?vt=passport%26vm=wpt%26ft=loginpage%26target=&style=25&low_login=1&proxy_url=https://mail.qq.com/proxy.html&need_qr=0&hide_border=1&border_radius=0&self_regurl=http://zc.qq.com/chs/index.html?type=1&app_id=11005?t=regist&pt_feedback_link=http://support.qq.com/discuss/350_1.shtml&css=https://res.mail.qq.com/zh_CN/htmledition/style/ptlogin_input1f1a66.css",
                    Encoding = Encoding.UTF8,
                    IsToLower = false,
                    Method = "get",
                    UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
                    ContentType = "application/x-www-form-urlencoded",
                    ResultType = ResultType.String,
                    Timeout = 50000,
                    ResultCookieType = ResultCookieType.CookieCollection
                };
                _HttpResult = _HttpHelper.GetHtml(_HttpItem);
                login_sig = QQHelper2.GetBetween(_HttpResult.Html, "login_sig:\"", "\",clientip:");
                HttpCookie.Add( _HttpResult.CookieCollection);

              

                _HttpItem = new HttpItem
                {
                    Allowautoredirect = false,
                    URL = string.Format("https://ssl.ptlogin2.qq.com/check?regmaster=&uin={0}&appid=522005705&js_ver=10085&js_type=1&login_sig={1}&u1=https%3A%2F%2Fmail.qq.com%2Fcgi-bin%2Flogin%3Fvt%3Dpassport%26vm%3Dwpt%26ft%3Dloginpage%26target%3D&r={2}",
                                           QQNumber, login_sig, QQBottleHelper.GetRandrom(18)),

                    Encoding = Encoding.UTF8,
                    IsToLower = false,
                    Method = "get",
                    UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
                    ContentType = "application/x-www-form-urlencoded",
                    ResultType = ResultType.String,
                    Timeout = 50000,
                    ResultCookieType = ResultCookieType.CookieCollection,
                    CookieCollection=HttpCookie
                };

                _HttpResult = _HttpHelper.GetHtml(_HttpItem);
                HttpCookie.Add(_HttpResult.CookieCollection);

                string[] ReturnNumber = _HttpResult.Html.Split(new string[] { "','", "', '" }, StringSplitOptions.None);
                VeryCode = ReturnNumber[1];
                Ptuin = ReturnNumber[2];

                if (VeryCode.Length > 4)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }



                #endregion

            //}
            //catch (Exception er)
            //{
            //    ErrMessage = er.Message;
            //    return -2;
            //}
        }
        #region 获取验证码图片
        public Image GetVeryCode()
        {


            _HttpItem = new HttpItem
            {
                Allowautoredirect = false,
                URL = string.Format("https://ssl.captcha.qq.com/getimage?uin={0}&aid=522005705&cap_cd={1}&{2}",
            QQNumber, VeryCode, QQBottleHelper.GetRandrom(17)),

                Encoding = Encoding.UTF8,
                IsToLower = false,
                Method = "get",
                UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
                ContentType = "application/x-www-form-urlencoded",
                ResultType = ResultType.Byte,
                Timeout = 50000,
                ResultCookieType = ResultCookieType.CookieCollection,
                CookieCollection=HttpCookie
            };
            _HttpResult = _HttpHelper.GetHtml(_HttpItem);

            HttpCookie.Add(_HttpResult.CookieCollection);
        

            return byteArrayToImage(_HttpResult.ResultByte);

        }
        #endregion
        public int LoginQQMail()
        {
            try
            {
                string sScript = Properties.Resources.QQMailPasswordJs;

                sScript = sScript.Replace("ptuin", Ptuin);
                ScriptEngine se = new ScriptEngine(ScriptLanguage.JavaScript);
                object[] paramers = new object[2] { QQPassword, VeryCode };
                object obj = se.Run("getEncryption", paramers, sScript);
                QQPassword = obj.ToString();

                _HttpItem = new HttpItem
           {
               Allowautoredirect = false,
               URL = string.Format("https://ssl.ptlogin2.qq.com/login?u={0}&verifycode={1}&p={2}&pt_rsa=0&u1=https%3A%2F%2Fmail.qq.com%2Fcgi-bin%2Flogin%3Fvt%3Dpassport%26vm%3Dwpt%26ft%3Dloginpage%26target%3D%26account%3D{0}&ptredirect=1&h=1&t=1&g=1&from_ui=1&ptlang=2052&action=2-74-1406134725843&js_ver=10085&js_type=1&login_sig={3}&pt_uistyle=25&aid=522005705&daid=4&",
                 QQNumber, VeryCode, QQPassword, login_sig),

               Encoding = Encoding.UTF8,
               IsToLower = false,
               Method = "get",
               UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
               ContentType = "application/x-www-form-urlencoded",
               ResultType = ResultType.String,
               Timeout = 50000,
               ResultCookieType = ResultCookieType.CookieCollection,
               CookieCollection=HttpCookie
           };
                _HttpResult = _HttpHelper.GetHtml(_HttpItem);
                HttpCookie.Add(_HttpResult.CookieCollection);
       

                if (_HttpResult.Html.Contains("验证码"))
                {
                    ErrMessage = "错误:验证码输入错误";
                    return -2;
                }
                if (_HttpResult.Html.Contains("密码"))
                {
                    ErrMessage = "错误:密码或者用户名输入错误";
                    return -3;
                }
                ErrMessage = _HttpResult.Html;


                //获取Location和cOOKIE
                string[] ReturnNumber = _HttpResult.Html.Split(new string[] { "','", "', '" }, StringSplitOptions.None);

                _HttpItem = new HttpItem
                {
                    Allowautoredirect = false,
                    URL = ReturnNumber[2],

                    Encoding =  Encoding.GetEncoding("gb18030"),
                    IsToLower = false,
                    Method = "get",
                    UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
                    ContentType = "application/x-www-form-urlencoded",
                    ResultType = ResultType.String,
                    Timeout = 50000,
                    ResultCookieType = ResultCookieType.CookieCollection,
                    CookieCollection = HttpCookie
                };
                _HttpResult = _HttpHelper.GetHtml(_HttpItem);
                HttpCookie.Add(_HttpResult.CookieCollection);
                ErrMessage = _HttpResult.Html;

                _HttpItem = new HttpItem
                {
                    Allowautoredirect = false,
                    URL = _HttpResult.Header["Location"],

                    Encoding = Encoding.GetEncoding("gb18030"),
                    IsToLower = false,
                    Method = "get",
                    UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
                    ContentType = "application/x-www-form-urlencoded",
                    ResultType = ResultType.String,
                    Timeout = 50000,
                    ResultCookieType = ResultCookieType.CookieCollection,
                    CookieCollection = HttpCookie
                };
                _HttpResult = _HttpHelper.GetHtml(_HttpItem);
                HttpCookie.Add(_HttpResult.CookieCollection);
                ErrMessage = _HttpResult.Html;

                _HttpItem = new HttpItem
                {
                    Allowautoredirect = false,
                    URL = _HttpResult.Header["Location"],

                    Encoding = Encoding.GetEncoding("gb18030"),
                    IsToLower = false,
                    Method = "get",
                    UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
                    ContentType = "application/x-www-form-urlencoded",
                    ResultType = ResultType.String,
                    Timeout = 50000,
                    ResultCookieType = ResultCookieType.CookieCollection,
                    CookieCollection = HttpCookie
                };
                _HttpResult = _HttpHelper.GetHtml(_HttpItem);
                HttpCookie.Add(_HttpResult.CookieCollection);

                ErrMessage = _HttpResult.Html;

                return 0;

            }
            catch
            {
                return 0;
            }

        }
        private Image byteArrayToImage(byte[] Bytes)
        {
            MemoryStream ms = new MemoryStream(Bytes);
            Image outputImg = Image.FromStream(ms);
            return outputImg;
        }







    }



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-7-28 19:08:46 | 显示全部楼层
字符串的cookie不能用集合,只能用字符串类型
回复

使用道具 举报

 楼主| 发表于 2014-7-28 20:44:57 | 显示全部楼层
站长苏飞 发表于 2014-7-28 19:08
字符串的cookie不能用集合,只能用字符串类型

使用字符串的cookie进行post没成功比用集合cookie 的post还是失败!
回复

使用道具 举报

发表于 2014-7-29 08:29:46 | 显示全部楼层
失败就要找失败的原因
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-24 14:50

© 2014-2021

快速回复 返回顶部 返回列表