苏飞论坛

标题: 还有多少人不知道Httphelper的CookieCollection集合Cookie使用方法 [打印本页]

作者: 站长苏飞    时间: 2018-11-22 13:07
标题: 还有多少人不知道Httphelper的CookieCollection集合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.CookieCollection
            };
            //请求的返回值对象
            HttpResult result = http.GetHtml(item);
            //获取请求的Cookie
            CookieCollection cookie = 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",//返回类型    可选项有默认值   
                CookieCollection = cookie,//把Cookie写入请求串中
                ResultCookieType = ResultCookieType.CookieCollection
            };
            //请求的返回值对象
            result = http.GetHtml(item);

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


上面是关于Httphelper的CookieCollection格式Cookie的一个使用方法大家参考一下
关键就在于参数里的
[C#] 纯文本查看 复制代码
ResultCookieType = ResultCookieType.CookieCollection

设置为CookieCollection时,不仅仅是返回字符串的Cookie,还会返回格式 为CookieCollection格式的Cookie,更方便大家的使用和操作。
使用的时候也很简单
同样设置一个Cookie的类型,然后直接赋值即可
[C#] 纯文本查看 复制代码
                CookieCollection = cookie,//把Cookie写入请求串中
                ResultCookieType = ResultCookieType.CookieCollection


作者: 范范    时间: 2018-11-22 15:23
路过, 支持楼主
作者: liu    时间: 2018-11-22 18:26
我只是路过打酱油的。
作者: 竹林风    时间: 2018-12-16 22:20
感谢分享




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