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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 15168|回复: 7

[更新] Httphelper万能框架新增CookieContainer 格式Cookie使用方案

[复制链接]
发表于 2018-11-27 16:38:39 | 显示全部楼层 |阅读模式
Httphelper万能框架新增CookieContainer 格式Cookie使用方案

导读部分


【HttpHelper万能框架】教程目录贴  http://www.sufeinet.com/thread-9989-1-1.html
【HttpHelper万能框架】源码购买帖  http://www.sufeinet.com/thread-9926-1-1.html

教程部分


CookieContainer格式 的Cookie用时候也是很常用的,毕竟格式比较规范一些,但由于.net框架只返回的字符串和CookieCollection格式Cookie,因此我在这里直接做个转化,方便大家使用,到此框架也算是支持三种格式的Cookie了


请求

时代码如下
[C#] 纯文本查看 复制代码
            //创建Httphelper对象
            HttpHelper http = new HttpHelper();
            //创建Httphelper参数对象
            HttpItem item = new HttpItem()
            {
                URL = "http://www.sufeinet.com",//URL     必需项    
                Method = "get",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                ResultCookieType = ResultCookieType.CookieContainer
            };
            //请求的返回值对象
            HttpResult result = http.GetHtml(item);


转化


代码
[C#] 纯文本查看 复制代码
  //获取请求的Cookie
            CookieContainer cookie =new CookieContainer();

            //将返回的CookieCollection转成CookieContainer格式就行了。
            cookie.Add(result.CookieCollection);

其实说白了CookieContainer是可以包含多个CookieCollection的大集合
而CookieCollection是可以包含多个字符串Cookie的集合

二次使用

接收和转化完成之后我们就可以直接使用了
[C#] 纯文本查看 复制代码
  // 第二次使用Cookie

            //创建Httphelper参数对象
            item = new HttpItem()
            {
                URL = "http://www.sufeinet.com/thread-9989-1-1.html",//URL     必需项    
                Method = "get",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                CookieContainer = cookie,//把Cookie写入请求串中
                ResultCookieType = ResultCookieType.CookieContainer
            };
            //请求的返回值对象
            result = http.GetHtml(item);

            //获取Html
            string html = result.Html;


全部代码如下
[C#] 纯文本查看 复制代码
 //创建Httphelper对象
            HttpHelper http = new HttpHelper();
            //创建Httphelper参数对象
            HttpItem item = new HttpItem()
            {
                URL = "http://www.sufeinet.com",//URL     必需项    
                Method = "get",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                ResultCookieType = ResultCookieType.CookieContainer
            };
            //请求的返回值对象
            HttpResult result = http.GetHtml(item);
            //获取请求的Cookie
            CookieContainer cookie =new CookieContainer();

            //将返回的CookieCollection转成CookieContainer格式就行了。
            cookie.Add(result.CookieCollection);

            // 第二次使用Cookie

            //创建Httphelper参数对象
            item = new HttpItem()
            {
                URL = "http://www.sufeinet.com/thread-9989-1-1.html",//URL     必需项    
                Method = "get",//URL     可选项 默认为Get   
                ContentType = "text/html",//返回类型    可选项有默认值   
                CookieContainer = cookie,//把Cookie写入请求串中
                ResultCookieType = ResultCookieType.CookieContainer
            };
            //请求的返回值对象
            result = http.GetHtml(item);

            //获取Html
            string html = result.Html;


好了就这样,
大家有什么问题请给我留言



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2018-11-27 16:57:32 | 显示全部楼层
【万能框架教程】- CookieCollection类型Cookie使用方法
http://www.sufeinet.com/thread-10017-1-1.html
(出处: 苏飞论坛)


CookieCollection类型Cookiel转为字符串Cookie
http://www.sufeinet.com/thread-10021-1-1.html
(出处: 苏飞论坛)


字符串Cookie转为CookieCollection类型Cookie
http://www.sufeinet.com/thread-10020-1-1.html
(出处: 苏飞论坛)



发表于 2018-11-27 16:58:00 | 显示全部楼层
学习了
回复

使用道具 举报

发表于 2018-11-27 21:28:10 | 显示全部楼层
先收藏,很不错的,以后慢慢学习。楼主加油
发表于 2018-11-27 21:53:11 | 显示全部楼层
写的很详细,强烈支持楼主ing……
发表于 2018-11-28 17:32:00 | 显示全部楼层
看到这帖子真是高兴!  学习下httphelper, 加油
发表于 2019-8-22 16:49:06 | 显示全部楼层

看到这帖子真是高兴!  学习下httphelper, 加油
发表于 2021-4-8 14:58:51 | 显示全部楼层
有自动更新cookie的方法吗 有时候并不是一次登录授权 要在好几个地方获取cookie
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-3-29 17:28

© 2014-2021

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