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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 7072|回复: 2

[HttpHelper] 关于httphelper的cookie的获取与处理 应该怎么做比较好呢

[复制链接]
发表于 2014-6-11 11:14:53 | 显示全部楼层 |阅读模式
1金钱
[C#] 纯文本查看 复制代码
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "http://www.baidu.com",//URL     必需项    
                Encoding = System.Text.Encoding.GetEncoding( "utf-8"),//URL     可选项 默认为Get   
                Method = "get",//URL     可选项 默认为Get   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写   
                Cookie = "",//字符串Cookie     可选项   
                Referer ="",//来源URL     可选项   
                Postdata = "",//Post数据     可选项GET时不需要写   
                Timeout = 100000,//连接超时时间     可选项默认为100000    
                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "text/html",//返回类型    可选项有默认值   
                Allowautoredirect = true,//是否根据301跳转     可选项   
                //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数   
                //Connectionlimit = 1024,//最大连接数     可选项 默认为1024    
                ProxyIp = "",//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数    
                //ProxyPwd = "123456",//代理服务器密码     可选项    
                //ProxyUserName = "administrator",//代理服务器账户名     可选项   
                ResultType = ResultType.Byte
            };
            HttpResult result = http.GetHtml(item);
            string cookie = string.Empty;
            foreach (CookieItem s in HttpCookieHelper.GetCookieList(result.Cookie))
            {
                if (s.Key.Contains("BAIDUID") || s.Key.Contains("BDSVRTM") || s.Key.Contains("H_PS_PSSID"))
                {
                    cookie += HttpCookieHelper.CookieFormat(s.Key, s.Value);
                }
            }
            string session = cookie;
           // string html = result.Html;
            item = new HttpItem()
            {
                URL = "https://passport.baidu.com/v2/api/?getapi&tpl=qing&apiver=v3&tt=1372662810579&class=login&callback=bd__cbs__3jptzw",//URL     必需项    
                Encoding = System.Text.Encoding.GetEncoding("utf-8"),//URL     可选项 默认为Get   
                Method = "get",//URL     可选项 默认为Get   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写   
                Cookie = session,//字符串Cookie     可选项   
                Referer = "http://www.baidu.com",//来源URL     可选项   
                Postdata = "",//Post数据     可选项GET时不需要写   
                Timeout = 100000,//连接超时时间     可选项默认为100000    
                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "text/html",//返回类型    可选项有默认值   
                Allowautoredirect = false,//是否根据301跳转     可选项   
                //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数   
                //Connectionlimit = 1024,//最大连接数     可选项 默认为1024    
                ProxyIp = "",//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数    
                //ProxyPwd = "123456",//代理服务器密码     可选项    
                //ProxyUserName = "administrator",//代理服务器账户名     可选项   
                ResultType = ResultType.Byte
            };
            foreach (CookieItem s in HttpCookieHelper.GetCookieList(result.Cookie))
            {
                if (s.Key.Contains("BAIDUID") || s.Key.Contains("HISTORY") || s.Key.Contains("Hm_lvt_90056b3f84f90da57dc0f40150f005d5") || s.Key.Contains("HOSUPPORT") || s.Key.Contains("H_PS_PSSID") || s.Key.Contains("SAVEUSERID") || s.Key.Contains("UBI") || s.Key.Contains("USERNAMETYPE"))
                {
                    session += HttpCookieHelper.CookieFormat(s.Key, s.Value);
                }
            }
            cookie= HttpCookieHelper.Addcookie(cookie, session);
            result = http.GetHtml(item);
            string tooken = Library.RegexHelper.regex_string_one("(?<=\"token\" : \").*?(?=\")", result.Html);
            session = cookie;
            item = new HttpItem()
            {
                URL = "https://passport.baidu.com/v2/api/?login",//URL     必需项    
                Encoding = System.Text.Encoding.GetEncoding("utf-8"),//URL     可选项 默认为Get   
                Method = "post",//URL     可选项 默认为Get   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写   
                Cookie = session,//字符串Cookie     可选项   
                Referer = "http://www.baidu.com/",//来源URL     可选项   
                Postdata = string.Format("staticpage=http%3A%2F%2Fhi.baidu.com%2Fcom%2Fshow%2Fproxy%3Ffor%3Dv3_pass_login&charset=utf-8&token={0}&tpl=qing&apiver=v3&tt=137{1}&codestring=&isPhone=false&safeflg=0&u=http%3A%2F%2Fhi.baidu.com%2F&username={2}&password={3}&verifycode=&mem_pass=on&ppui_logintime=165501&callback=parent.bd__pcbs__hggm3p", tooken, 2135548532, "shijiaxin3180", "test123456"),//Post数据 shijiaxin3180:test123456       可选项GET时不需要写   
                Timeout = 100000,//连接超时时间     可选项默认为100000    
                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "text/html",//返回类型    可选项有默认值   
                Allowautoredirect = false,//是否根据301跳转     可选项   
                //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数   
                //Connectionlimit = 1024,//最大连接数     可选项 默认为1024    
                ProxyIp = "",//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数    
                //ProxyPwd = "123456",//代理服务器密码     可选项    
                //ProxyUserName = "administrator",//代理服务器账户名     可选项   
                ResultType = ResultType.String
            };
            result = http.GetHtml(item);

            foreach (CookieItem s in HttpCookieHelper.GetCookieList(result.Cookie))
            {
                if (s.Key.Contains("BAIDUID") || s.Key.Contains("HISTORY") || s.Key.Contains("Hm_lvt_90056b3f84f90da57dc0f40150f005d5") || s.Key.Contains("HOSUPPORT") || s.Key.Contains("H_PS_PSSID") || s.Key.Contains("SAVEUSERID") || s.Key.Contains("UBI") || s.Key.Contains("USERNAMETYPE") || s.Key.Contains("BDUSS"))
                {
                    session += HttpCookieHelper.CookieFormat(s.Key, s.Value);
                }
            }
            cookie = HttpCookieHelper.Addcookie(cookie, session);
             richTextBox1.Text = cookie;

以为自己懂了httphelper的使用 原来还是不懂啊,
自己尝试了三四天 无论怎么更改cookie就是无法获取到登录的cookie “BDUSS” 使用了之前的老方法 一直都能顺利获取
[C#] 纯文本查看 复制代码
            //访问首页带上cookie
            HttpHelper.HttpGET("http://www.baidu.com/", "", "http://www.baidu.com/", 2, ref cookie, 20, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");

            //使用上一步的cookie访问api页面
            string html = HttpHelper.HttpGET("https://passport.baidu.com/v2/api/?getapi&tpl=qing&apiver=v3&tt=1372662810579&class=login&callback=bd__cbs__3jptzw", "", "http://www.baidu.com/", 2, ref cookie, 20, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
            tokenstr = html.Substring(html.IndexOf("token")+10);
            tokenstr = tokenstr.Remove(tokenstr.IndexOf("\""));
         
            //带上token和cookiepost登陆页面
            richTextBox1.Text = HttpHelper.HttpPOST("https://passport.baidu.com/v2/api/?login", "", "staticpage=http%3A%2F%2Fhi.baidu.com%2Fcom%2Fshow%2Fproxy%3Ffor%3Dv3_pass_login&charset=utf-8&token=" + tokenstr + "&tpl=qing&apiver=v3&tt=1372661616626&codestring=&isPhone=false&safeflg=0&u=http%3A%2F%2Fhi.baidu.com%2F&username=shijiaxin3180&password=test123456&verifycode=&mem_pass=on&ppui_logintime=165501&callback=parent.bd__pcbs__hggm3p", "http://www.baidu.com/", true, false, ref cookie, 20, "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)", false);
            if (richTextBox1.Text.Contains("captchaservice"))
            {
                string codeurl = richTextBox1.Text.Substring(richTextBox1.Text.IndexOf("captchaservice"));
                codeurl = codeurl.Remove(codeurl.IndexOf("&"));
                Image codeimg = HttpHelper.HttpGetImage("https://passport.baidu.com/cgi-bin/genimage?" + codeurl, "", "http://hi.baidu.com/", ref cookie, 20);
                code c = new code();
                c.codeimg = codeimg;
                c.ShowDialog();
                //带上token和cookiepost登陆页面
                richTextBox1.Text = HttpHelper.HttpPOST("https://passport.baidu.com/v2/api/?login", "", "staticpage=http%3A%2F%2Fhi.baidu.com%2Fcom%2Fshow%2Fproxy%3Ffor%3Dv3_pass_login&charset=utf-8&token=" + tokenstr + "&tpl=qing&apiver=v3&tt=1372661616626&codestring=" + codeurl + "&isPhone=false&safeflg=0&u=http%3A%2F%2Fhi.baidu.com%2F&username=shijiaxin3180&password=test123456&verifycode=" + c.codestr + "&mem_pass=on&ppui_logintime=165501&callback=parent.bd__pcbs__hggm3p", "http://hi.baidu.com/", true, false, ref cookie, 20, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)", false);
            }



httphelper.rar

3.55 KB, 下载次数: 99, 下载积分: 金钱 -1



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2014-6-12 14:17:17 | 显示全部楼层
手动设置了下获取cookie的属性 登录成功了 哈 看来都弄明白了

baidu.png
[C#] 纯文本查看 复制代码
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "http://www.baidu.com/",//URL     必需项    
            };
            HttpResult result = http.GetHtml(item);
            string html = result.Html;
            string cookie = result.Cookie;

            item = new HttpItem()
            {
                URL="http://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&tt=1402545719625&class=login&logintype=dialogLogin&callback=bd__cbs__secnga",
                Referer = "http://www.baidu.com/",//来源URL     可选项   
                Cookie = cookie,//字符串Cookie     可选项   

            };
            result = http.GetHtml(item);
            html = result.Html;
            string tooken = RegexHelper.regex_string_one("(?<=\"token\" : \").*?(?=\")", html);
            cookie = result.Cookie+"; "+item.Cookie;

            string cookiestring = string.Empty;
            foreach (CookieItem s in HttpCookieHelper.GetCookieList(cookie))
            {
                if (s.Key.Contains("BAIDUID") || s.Key.Contains("BDSVRTM") || s.Key.Contains("H_PS_PSSID") || s.Key.Contains("HOSUPPORT"))
                {
                    cookiestring = cookiestring + HttpCookieHelper.CookieFormat(s.Key, s.Value);
                }
            }

            item = new HttpItem()
            {
                URL = "http://passport.baidu.com/v2/api/?login",//URL     必需项    
                Method = "post",//URL     可选项 默认为Get   
                Cookie = cookiestring,//字符串Cookie     可选项   
                Referer = "http://www.baidu.com/",//来源URL     可选项   
                Postdata = "staticpage=http%3A%2F%2Fhi.baidu.com%2Fcom%2Fshow%2Fproxy%3Ffor%3Dv3_pass_login&charset=utf-8&token=" + tooken + "&tpl=qing&apiver=v3&tt=1372661616626&codestring=&isPhone=false&safeflg=0&u=http%3A%2F%2Fhi.baidu.com%2F&username=shijiaxin3180&password=test123456&verifycode=&mem_pass=on&ppui_logintime=165501&callback=parent.bd__pcbs__hggm3p",//Post数据     可选项GET时不需要写   
            };

            result = http.GetHtml(item);
            html = result.Html;

            if (html.Contains("captchaservice"))
            {
                string codeurl = html.Substring(html.IndexOf("captchaservice"));
                codeurl = codeurl.Remove(codeurl.IndexOf("&"));

                item = new HttpItem()
                {
                    URL = "https://passport.baidu.com/cgi-bin/genimage?" + codeurl,//URL     必需项    
                    Cookie = cookiestring,//字符串Cookie     可选项   
                    Referer = "http://www.baidu.com/",//来源URL     可选项   
                };
                result = http.GetHtml(item);
                Image codeimg = byteArrayToImage(result.ResultByte);
                code c = new code();
                c.codeimg = codeimg;
                c.ShowDialog();

                item = new HttpItem()
                {
                    URL = "http://passport.baidu.com/v2/api/?login",//URL     必需项    
                    Method = "post",//URL     可选项 默认为Get   
                    Cookie = cookiestring,//字符串Cookie     可选项   
                    Referer = "http://www.baidu.com/",//来源URL     可选项   
                    Postdata = "staticpage=http%3A%2F%2Fhi.baidu.com%2Fcom%2Fshow%2Fproxy%3Ffor%3Dv3_pass_login&charset=utf-8&token=" + tooken + "&tpl=qing&apiver=v3&tt=1372661616626&codestring=" + codeurl + "&isPhone=false&safeflg=0&u=http%3A%2F%2Fhi.baidu.com%2F&username=shijiaxin3180&password=test123456&verifycode=" + c.codestr + "&mem_pass=on&ppui_logintime=165501&callback=parent.bd__pcbs__hggm3p",//Post数据     可选项GET时不需要写   
                };

                result = http.GetHtml(item);
                html = result.Html;

            }

            richTextBox1.Text = html + "\r\n====================================================\r\n" + cookiestring + "\r\n====================================================\r\n" + result.Header.ToString();



回复

使用道具 举报

发表于 2015-7-15 11:19:32 | 显示全部楼层
强烈支持楼主ing……
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 23:07

© 2014-2021

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