none
c# 如何获取当前 页面 所有cookie 包括其他域名 RRS feed

  • 问题

  • cookie信息 如
    .bbs5.com TRUE / FALSE 2147385600 undefined pgv_pvid 1635122573
    .www1.bbs5.com TRUE / FALSE 1393469041 undefined randomSeed 826003
    localhost FALSE / FALSE 0 undefined Maopu cid=85858185 


    获取bbs5.com 的pgv_pvid 信息
    在获取.www1.bbs5.com 的randomSeed  信息
    2013年10月28日 8:09

答案

  • 你好:

    欢迎来到MSDN中文论坛!

    如果仅仅想获取当前站点的所有Cookie:

    string[] cookies = Request.Cookies.AllKeys;
                foreach (string cookie in cookies)
                {
                    HttpCookie c = Response.Cookies[cookie];
                }

    如果想要获取其他的站点的Cookie可能需要找到Cookie文件,从文件中读取。

    参考:http://www.codeproject.com/Articles/330142/Cookie-Quest-A-Quest-to-Read-Cookies-from-Four-Pop

    从ie获取Cookie的代码:

    private static bool GetCookie_InternetExplorer
        (string strHost, string strField, ref string Value)
    {
        Value = string.Empty;
        bool fRtn = false;
        string strPath, strCookie;
        string[] fp;
        StreamReader r;
        int idx;
    
        try
        {
            strField = strField + "\n";
            strPath = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
            Version v = Environment.OSVersion.Version;
    
            if (IsWindows7())
            {
                strPath += @"\low";
            }
    
            fp = Directory.GetFiles(strPath, "*.txt");
    
            foreach (string path in fp)
            {
                idx = -1;
                r = File.OpenText(path);
                strCookie = r.ReadToEnd();
                r.Close();
                
                if(System.Text.RegularExpressions.Regex.IsMatch(strCookie, strHost))
                {
                    idx = strCookie.ToUpper().IndexOf(strField.ToUpper());
                }
                
                if (-1 < idx)
                {
                    idx += strField.Length;
                    Value = strCookie.Substring(idx, strCookie.IndexOf('\n', idx) -idx);
                    if (!Value.Equals(string.Empty))
                    {
                        fRtn = true;
                        break;
                    }
                }
            }
        }
        catch (Exception) //File not found, etc...
        {
            Value = string.Empty;
            fRtn = false;
        }
    
        return fRtn;
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年10月29日 7:05
    版主

全部回复

  • 你好:

    欢迎来到MSDN中文论坛!

    如果仅仅想获取当前站点的所有Cookie:

    string[] cookies = Request.Cookies.AllKeys;
                foreach (string cookie in cookies)
                {
                    HttpCookie c = Response.Cookies[cookie];
                }

    如果想要获取其他的站点的Cookie可能需要找到Cookie文件,从文件中读取。

    参考:http://www.codeproject.com/Articles/330142/Cookie-Quest-A-Quest-to-Read-Cookies-from-Four-Pop

    从ie获取Cookie的代码:

    private static bool GetCookie_InternetExplorer
        (string strHost, string strField, ref string Value)
    {
        Value = string.Empty;
        bool fRtn = false;
        string strPath, strCookie;
        string[] fp;
        StreamReader r;
        int idx;
    
        try
        {
            strField = strField + "\n";
            strPath = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
            Version v = Environment.OSVersion.Version;
    
            if (IsWindows7())
            {
                strPath += @"\low";
            }
    
            fp = Directory.GetFiles(strPath, "*.txt");
    
            foreach (string path in fp)
            {
                idx = -1;
                r = File.OpenText(path);
                strCookie = r.ReadToEnd();
                r.Close();
                
                if(System.Text.RegularExpressions.Regex.IsMatch(strCookie, strHost))
                {
                    idx = strCookie.ToUpper().IndexOf(strField.ToUpper());
                }
                
                if (-1 < idx)
                {
                    idx += strField.Length;
                    Value = strCookie.Substring(idx, strCookie.IndexOf('\n', idx) -idx);
                    if (!Value.Equals(string.Empty))
                    {
                        fRtn = true;
                        break;
                    }
                }
            }
        }
        catch (Exception) //File not found, etc...
        {
            Value = string.Empty;
            fRtn = false;
        }
    
        return fRtn;
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年10月29日 7:05
    版主
  • 这个能在web 端文件 里面实现吗,兼容所有浏览器 ?
    2013年10月29日 12:10
  • Web页面访问客户端Cookie文件夹里面的文件?不可以!

    链接里面的代码示例包括多种浏览器,我回复的代码只是其中ie的部分。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2013年10月30日 1:14
    版主