wininet.dll函数库:不会过期的cookie

using System;

using System.Text;

using System.Runtime.InteropServices;

namespace seaven

{

    class Program

    {

        /// <summary>

        /// 设置cookie

        /// </summary>

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]

        public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);

        /// <summary>

        /// 获取cookie

        /// </summary>

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]

        public static extern bool InternetGetCookie(

        string url, string name, StringBuilder data, ref int dataSize);

        static void Main(string[] args)

        {

            //获取旧的

            StringBuilder cookie = new StringBuilder(new String(' ', 2048));

            int datasize = cookie.Length;

            bool b = InternetGetCookie("http://www.seaven.org", null, cookie, ref datasize);

            //删除旧的

            foreach (string fileName in System.IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.Cookies)))

            {

                if (fileName.ToLower().IndexOf("seaven") > 0)

                {

                    System.IO.File.Delete("seaven");

                }

            }

            //生成新的

            foreach (string c in cookie.ToString().Split(';'))

            {

                string[] item = c.Split('=');

                string name = item[0];

                string value = item[1] + ";expires=Sun,22-Feb-2099 00:00:00 GMT";

                InternetSetCookie("http://www.seaven.org", name, value);

                InternetSetCookie("http://www.seaven.org", name, value);

                InternetSetCookie("http://www.seaven.org", name, value);

            }

        }

    }

}

 

你可能感兴趣的:(cookie)