苏飞论坛

标题: Httphelper万能框架新增CookieContainer 格式Cookie使用方案 [打印本页]

作者: 站长苏飞    时间: 2018-11-27 16:38
标题: Httphelper万能框架新增CookieContainer 格式Cookie使用方案
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;


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


作者: 站长苏飞    时间: 2018-11-27 16:57
【万能框架教程】- 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
学习了
作者: 竹林风    时间: 2018-11-27 21:28
先收藏,很不错的,以后慢慢学习。楼主加油
作者: Amy    时间: 2018-11-27 21:53
写的很详细,强烈支持楼主ing……
作者: 范范    时间: 2018-11-28 17:32
看到这帖子真是高兴!  学习下httphelper, 加油
作者: wx_qdqS4wF4    时间: 2019-8-22 16:49

看到这帖子真是高兴!  学习下httphelper, 加油
作者: echoxxx    时间: 2021-4-8 14:58
有自动更新cookie的方法吗 有时候并不是一次登录授权 要在好几个地方获取cookie




欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4