using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// cookie 的摘要说明
/// </summary>
public class cookie
{
	public cookie()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}

    //cooknames:cookie文件的名称,cook_name:cookie存贮的单个文件的名称,cook_value:cookie存贮的单个文件的值,days:存贮天数
    public void wr_cookie(string cooknames,string cook_ad, string ad_value,string cook_pw,string pw_pvalue,string cook_file,string file_value, int days)
    {
        try
        {
            //if (System.Web.HttpContext.Current.Request.Cookies[cooknames] == null)
            //{
            //    HttpCookie addCookie = new HttpCookie(cooknames);
            //    addCookie[cook_name] = cook_value;
            //    addCookie.Expires = DateTime.Now.AddDays(days);//这里设置要保存多长时间
            //    System.Web.HttpContext.Current.Response.Cookies.Add(addCookie);
            //    System.Web.HttpContext.Current.Response.Write("1:" + cook_name);
            //}
            //else
            //{
            //if (System.Web.HttpContext.Current.Request.Cookies[cooknames].Values[cook_ad] == null)
            //{
                HttpCookie addCookie = new HttpCookie(cooknames);
                addCookie.Expires = DateTime.Now.AddDays(days);//这里设置要保存多长时间
                addCookie.Values.Add(cook_ad, System.Web.HttpUtility.UrlEncode(ad_value));
                addCookie.Values.Add(cook_pw, System.Web.HttpUtility.UrlEncode(pw_pvalue));
                addCookie.Values.Add(cook_file, System.Web.HttpUtility.UrlEncode(file_value));
                System.Web.HttpContext.Current.Response.AppendCookie(addCookie);
            //}
            //}
        }
        catch (System.Exception ee)
        {
            System.Web.HttpContext.Current.Response.Write("存贮入cookie"+ee.Message.ToString());
        }
    }

    public string dr_cookie(string cooknames, string cook_name)
    {
        string name = "";
        try
        {
            if (System.Web.HttpContext.Current.Request.Cookies[cooknames] != null)
            {
                HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies[cooknames];
                name = System.Web.HttpUtility.UrlDecode(cookie.Values[cook_name].ToString());
            }
        }
        catch (System.Exception ee)
        {
            System.Web.HttpContext.Current.Response.Write("读取cookie" + ee.Message.ToString());
        }
        return name;
    }
}
