-->

InternetSetCookie does not store cookie in Tempora

2019-06-01 03:26发布

问题:

I'm trying to create a cookie on client side using wininet from a c# winform application. So I use this code:

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]  
public static extern bool InternetSetCookie
(
    string lpszUrlName, 
    string lbszCookieName, 
    string lpszCookieData
);

private void btnRestaure_Click(object sender, EventArgs e)
{
    try
    {
        var result = InternetSetCookie("www.mydomain.com", "MyCookie",
                              "value=helloworld");
    }
    catch(Exception ex)
    {

    }
}

InternetSetCookie returns me true but I can't find the cookie in the Temporary Internet Files folder. Any clue ?

回答1:

I think if you don't specify an expiration date then you create a session cookie. If you want a persistent cookie try specifying an expiration date.

I assume that such a cookie is only stored in RAM and not serialized to disk. And most likely only visible to your own process.


If you check the MSDN documentation for InternetSetCookie you see:

Cookies created by InternetSetCookie without an expiration date are stored in memory and are available only in the same process that created them. Cookies that include an expiration date are stored in the windows\cookies directory.