比如我使用IE登录了人人网,然后使用了下次自动登录,所以相关Cookie存储了相应的信息。然后,我自己写了一个WinForm程序,此时,我能否通过C#去读取IE下的Cookie进行分析,然后使用WebClint或者HttpWebRequest进行登录呢?

解决方案 »

  1.   

    可以的,读取文件后,自行设置cookies
    但现在的浏览器Cookie不好提取的,IE6是可以的
      

  2.   

    网上找下cookies的内容,可以读取
      

  3.   


    Cookie一般是以txt文本形式存在的,其内部内容没有加密么?
      

  4.   

    不记得什么时候写的了,仅供参考,楼主可以搜索一下“IE cookie 格式”
            public static list<Cookie> get(string url, bool isRemove)
            {
                list<Cookie> cookies = new list<Cookie>();
                if (url != null && url.Length != 0)
                {
                    int index = url.IndexOf("://", StringComparison.Ordinal);
                    if (index != -1)
                    {
                        int pathIndex = url.IndexOf('/', index += 3);
                        string urlDomain = pathIndex == -1 ? url.Substring(index) : url.Substring(index, pathIndex - index);
                        if (urlDomain.Length != 0)
                        {
                            bool isCookie;
                            Cookie newCookie;
                            int lines, startIndex, removeCount;
                            long expiresLow, expiresHigh;
                            DateTime expires;
                            string[] cookieInfos;
                            bool[] isRemoves;
                            string domain, path, urlPath = pathIndex == -1 ? "/" : url.Substring(pathIndex);
                            urlDomain = "." + urlDomain;
                            foreach (string filename in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Cookies), "*.txt"))
                            {
                                try
                                {
                                    lines = (cookieInfos = File.ReadAllLines(filename)).Length;
                                    isRemoves = isRemove ? new bool[lines] : null;
                                    for (startIndex = lines - 8, removeCount = index = 0; index <= startIndex; index++)
                                    {
                                        isCookie = false;
                                        if (long.TryParse(cookieInfos[index + 4], out expiresLow)
                                            && long.TryParse(cookieInfos[index + 5], out expiresHigh))
                                        {
                                            expires = DateTime.FromFileTimeUtc((expiresHigh << 32) + expiresLow);
                                            if (expires > date.NowTime)
                                            {
                                                if ((pathIndex = (domain = cookieInfos[index + 2]).IndexOf('/')) == -1)
                                                {
                                                    path = "/";
                                                }
                                                else
                                                {
                                                    path = domain.Substring(pathIndex);
                                                    domain = domain.Substring(0, pathIndex);
                                                }
                                                if (domain[0] != '.') domain = "." + domain;
                                                //if ((domain[0] == '.' ? urlDomain.EndsWith(domain, StringComparison.OrdinalIgnoreCase) || String.Compare(urlDomain, 0, domain, 1, urlDomain.Length, true) == 0 : String.Compare(urlDomain, domain, true) == 0) && urlPath.StartsWith(path, StringComparison.OrdinalIgnoreCase))
                                                isCookie = urlDomain.EndsWith(domain, StringComparison.OrdinalIgnoreCase) && urlPath.StartsWith(path, StringComparison.OrdinalIgnoreCase);
                                                if (isCookie && cookieInfos[index].Length != 0)
                                                {
                                                    newCookie = new Cookie(fastCSharp.web.cookie.FormatCookieName(cookieInfos[index]), fastCSharp.web.cookie.FormatCookieValue(cookieInfos[index + 1]), path, domain);
                                                    cookies.Add(newCookie);
                                                    newCookie.Expires = expires;
                                                }
                                            }
                                        }
                                        if (isRemove && isCookie)
                                        {
                                            removeCount++;
                                            isRemoves[index] = isRemoves[index + 1] = isRemoves[index + 2]
                                                = isRemoves[index + 3] = isRemoves[index + 4] = isRemoves[index + 5]
                                                = isRemoves[index + 6] = isRemoves[index + 7] = true;
                                            index += 8;
                                            while (index < lines
                                                && (cookieInfos[index].Length != 1 || cookieInfos[index][0] != '*'))
                                            {
                                                isRemoves[index++] = true;
                                            }
                                            if (index < lines) isRemoves[index] = true;
                                        }
                                        else
                                        {
                                            index += 8;
                                            while (index < lines
                                                && (cookieInfos[index].Length != 1 || cookieInfos[index][0] != '*'))
                                            {
                                                index++;
                                            }
                                        }
                                    }
                                    if (removeCount != 0)
                                    {
                                        stringBuilder cookieText = new stringBuilder();
                                        for (index = removeCount = 0; index < lines; ++index)
                                        {
                                            if (!isRemoves[index])
                                            {
                                                removeCount++;
                                                cookieText.Add(cookieInfos[index]);
                                            }
                                        }
                                        File.Delete(filename);
                                        File.WriteAllText(filename, cookieText.ToString());
                                    }
                                }
                                catch (Exception error)
                                {
                                    log.Default.Add(error, "cookie文件 " + filename + " 相关操作失败", false);
                                }
                            }
                        }
                    }
                }
                return cookies;
            }
      

  5.   

    像搜狗啊,360啊,这些浏览器用的都是IE的内核,那Cookie啥的,跟IE一样么,求教
      

  6.   

    如果你没有选择“记住密码”一类的选项,是没法登录的,CCTV有些骇人听闻了。