当前位置:编程学习 > C#/ASP.NET >>

获取Cookie的代码,各位帮我看看哪里出错了?


  public static string ReadGoogleCookie()
       {

           #region 读出Cookie的index.dat 文件
           string dir = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
           Uri uri = new Uri(dir + @"\index.dat");
           FileStream fr = new FileStream(uri.LocalPath , FileMode.Open , FileAccess.Read  , FileShare.ReadWrite);
           int FileSize = Convert.ToInt32(fr.Length);
           byte[] bytes = new byte[FileSize];
           fr.Read(bytes, 0, FileSize);
           fr.Close();
           fr.Dispose();

           StringBuilder sb = new StringBuilder();
        //   bool LastIsZero = false;
           foreach (byte oneByte in bytes)
           { sb.Append(oneByte); }
           string indexFileString = sb.ToString();

           #endregion

           #region 读出Google 的所有文件的所有Cookie 信息
           //MatchCollection mc = Regex.Matches(indexFileString, @"Cookie:[^@]+@tompda\.com([^\n]+)\n([^\n]+)\n", RegexOptions.Singleline);
 MatchCollection mc = Regex.Matches(indexFileString, @"Cookie:[^@]+@tompda.com([^\n]+)\n([^\n]+)\n", RegexOptions.Singleline);
           String CookieInfomation = "";
           foreach (Match m in mc)
           {
               try
               {
                   //获得Cookie 文件的基本信息
                   string Domian = "tompda.com";
                   string path = m.Groups[1].ToString();
                   string CookieFileName = m.Groups[2].ToString();

                   //读入具体的一个Cookie 文件
               
                   StreamReader sr = new StreamReader(new Uri(uri, CookieFileName).LocalPath, Encoding.ASCII);
                   string FileContent = sr.ReadToEnd();
                   string[] CookieSplit = FileContent.Split(new string[] { "*\n" }, StringSplitOptions.RemoveEmptyEntries);
                   sr.Close();
                   sr.Dispose();
                   foreach (string oneCookie in CookieSplit)
                   {
                       //不考虑过期,只读出其中的值,也不特别地考虑是否是HttpOnly 的Cookie
                       string[] CookiePiece = oneCookie.Split('\n');
                       if (CookiePiece.Length > 5)
                       {
                           CookieInfomation += "<Begin>" + Domian + "<|>" + path + "<|>";
                           string Name = CookiePiece[0];
                           string Value = CookiePiece[1];
                           CookieInfomation += Name + "=" + Value + ";";
                           CookieInfomation += "<End>";
                       }
                   }
               }
               catch
               {
               }
           }
           #endregion

           return CookieInfomation;
       }
    }


这是网上找的代码,读取本地Cookie文件获取的,但怎么读取出来的都是乱码呢???
哪位高手帮忙解决一下吧 --------------------编程问答-------------------- 有人帮帮忙吗 --------------------编程问答-------------------- 我以前做过一个使用本地Api函数,叫做InternetGetCookie
这个函数可以找到本地保存的cookie。
你的这个,既然读取到了数据,估计也可以,如果是乱码的话,就是在编码方面出了问题!
你看一下,打开本地文件使用的是什么编码,然后处理过程中是什么编码。
ascii和unicode之间的编码转换,很有必要! --------------------编程问答-------------------- 读取时指定编码
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,