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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 4789|回复: 3

[例子] 站长看过来,自己封装的Cooies 共享和同步类

[复制链接]
发表于 2015-8-25 12:50:50 | 显示全部楼层 |阅读模式



虽然站长的httphelper 类已经很完美.但是我觉得不好的地方就是cookies处理问题..  之前因为这个问题导致BUG让我搞了一个星期

  我的类有以下特点..只需要创建一次..然后get post 都实现cookies的同步


而且每次请求都会带上上次同步好的cookies  

注意: 不是简单的同步
  一些网址不返回cookies 或是单纯的改变某key的值..这时 你不可能完全覆盖 上次请求返回的cookies  这时我的类就派上用场了


  不废话了上代码
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Class_tools
{
    /// <summary>
    /// 支持Cookies共享的 http类 该类请求.所有的Cookies 都会同步更新的
    /// </summary>
    public class Tu_MyHttp
    {
        /// <summary>
        /// 共享的cookies
        /// </summary>
        public string Cookies { set; get; }

        /// <summary>
        /// 浏览器标识
        /// </summary>
        public string UserAgent { set; get; }

        public string SeedGet(string url)
        {
            HttpItem item = new HttpItem();
            item.URL = url;
            return SeedHttpItem(item).Html;
        }
        public string SeedPost(string url, string postData)
        {
            HttpItem item = new HttpItem();
            item.URL = url;
            item.Method = "POST";
            item.Postdata = postData;
            return SeedHttpItem(item).Html;
        }

        public HttpResult SeedHttpItem(HttpItem item)
        {
            item.Cookie = Cookies;
            if (!string.IsNullOrWhiteSpace(UserAgent))
            {
                item.UserAgent = UserAgent;
            }
            HttpResult result = Tu_web.GetResult(item);
            string cookies = Cookies;

            Tu_web.UpdateCookies(result, ref cookies);
            if (!string.IsNullOrWhiteSpace(cookies))
            {
                Cookies = cookies;
            }
           
            return result;

        }
    }
}


核心方法 更新cookies方法,cookies 这些都会自动简化  因为本人封闭的类也调用了其他很多字符串处理小方法..所以这些请按个人理解自己做


[C#] 纯文本查看 复制代码
  public static void UpdateCookies(HttpResult itemT,ref string cookies)
        {


            if (string.IsNullOrWhiteSpace(itemT.Cookie))
            {
                return;
            }
            
            if (string.IsNullOrWhiteSpace(itemT.Cookie))
            {
                return;
            }

            Dictionary<string, string> DicCookies = new Dictionary<string, string>();

            ////临时测试
            //cookies = itemT.Cookie;
            //return;
            itemT.Cookie = cookies + ";" + itemT.Cookie;
            
            string[] rows = itemT.Cookie.Split(';');

            foreach (var item in rows)
            {
                if (!string.IsNullOrWhiteSpace(item))
                {
                    string newrow = item;
                    //newrow = newrow.Replace("httponly,", "").Trim();
                    string name = "";
                    string value = "";
                    if (newrow.Contains("="))
                    {
                        name = Tu_string.GetLeft(newrow, "=").Trim();
                        value = Tu_string.GetRight(newrow, "=").Trim();
                    }
                    if (name == "")
                    {
                        name = newrow;
                    }
                    if (name.Contains(","))
                    {
                        string temp = name;
                        name = Tu_string.GetRight(name, ",",true);
                        if (string.IsNullOrWhiteSpace(name))
                        {
                            name = temp;
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(name) && name != "expires" && value != "deleted" && !name.Contains("/"))
                    {
                        if (DicCookies.ContainsKey(name))
                        {
                            DicCookies[name] = value;
                        }
                        else
                        {
                            DicCookies.Add(name, value);
                        }
                    }
                }
            }
 
            string newCookies = "";
            foreach (var item in DicCookies)
            {
                newCookies += item.Key + "=" + item.Value + "; ";
            }
            cookies = newCookies.Trim().TrimEnd(';');

            itemT.Cookie = cookies;
        }







1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2015-8-25 13:00:29 | 显示全部楼层
本帖最后由 Cheungnotes 于 2015-8-25 13:02 编辑

膜拜中....!
至少要发个DEMO呀

发表于 2015-8-25 13:04:36 | 显示全部楼层
我只是路过打酱油的。

挺好的,继续努力
发表于 2018-7-27 15:41:26 | 显示全部楼层
本帖最后由 131415 于 2018-7-27 15:44 编辑

论坛的教程,对新手都不太友好。代码肯定不错,就是不会用。不知道如何调用到httpHelper类中。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-4-20 15:16

© 2014-2021

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