百度空间 | 百度首页 
 
查看文章
 
wininet.dll函数库:不会过期的cookie
2008-09-11 17:56

wininet.dll中包含很多win32下和网络有关的函数,包括internet,ftp等,下面演示一个IE下不过期的cookie。 比如csdn的登陆信息可以保存2个星期,你在登陆后把系统时间改为2周后,登陆信息就失效了,使用InternetSetCookie可以自己设置过期日期。 首先在IE中登陆,登陆时选择信息保存2周,然后运行如下代码,运行之后你可以把日期调整到2010年看效果:

  1. using System;   
  2.   
  3. using System.Text;   
  4.   
  5. using System.Runtime.InteropServices;   
  6.   
  7. namespace ConsoleApplication1   
  8.   
  9. {   
  10.   
  11.     class Program   
  12.   
  13.      {   
  14.   
  15.         /// <summary>   
  16.   
  17.         /// 设置cookie   
  18.   
  19.         /// </summary>   
  20.   
  21.          [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]   
  22.   
  23.         public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);   
  24.   
  25.         /// <summary>   
  26.   
  27.         /// 获取cookie   
  28.   
  29.         /// </summary>   
  30.   
  31.          [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]   
  32.   
  33.         public static extern bool InternetGetCookie(   
  34.   
  35.           string url, string name, StringBuilder data, ref int dataSize);   
  36.   
  37.         static void Main(string[] args)   
  38.   
  39.          {   
  40.   
  41.             //获取旧的   
  42.   
  43.              StringBuilder cookie = new StringBuilder(new String(' ',2048));   
  44.   
  45.             int datasize = cookie.Length;   
  46.   
  47.             bool b= InternetGetCookie("http://community.csdn.net", null, cookie, ref datasize);   
  48.   
  49.             //删除旧的   
  50.   
  51.             foreach (string fileName in System.IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.Cookies)))   
  52.   
  53.              {   
  54.   
  55.                 if (fileName.ToLower().IndexOf("csdn") > 0)   
  56.   
  57.                  {   
  58.   
  59.                      System.IO.File.Delete("csdn");   
  60.   
  61.                  }   
  62.   
  63.              }   
  64.   
  65.             //生成新的   
  66.   
  67.             foreach (string c in cookie.ToString().Split(';'))   
  68.   
  69.              {   
  70.   
  71.                 string[] item = c.Split('=');   
  72.   
  73.                 string name = item[0];   
  74.   
  75.                 string value = item[1] + ";expires=Sun,22-Feb-2099 00:00:00 GMT";   
  76.   
  77.                  InternetSetCookie("http://community.csdn.net",name,value);   
  78.   
  79.                  InternetSetCookie("http://forum.csdn.net", name, value);   
  80.   
  81.                  InternetSetCookie("http://webim.csdn.net", name, value);   
  82.   
  83.              }   
  84.   
  85.          }   
  86.   
  87.      }   
  88.   
  89. }  

类别:c# | 添加到搜藏 | 浏览() | 评论 (1)
 
最近读者:
 
网友评论:
1
2008-10-22 13:07 | 回复
草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的 草你妈 再瞎发广告 干死你妈比的
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu