Important:
This is retired content. This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This content may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.
A version of this page is also available for
4/8/2010

The following table shows Windows Internet Services (WinInet) functions that allow an application to create or retrieve cookies in the cookie database.

Function Description See

InternetGetCookie

Retrieves cookies for the specified URL and all of its parent URLs.

Getting a Cookie

InternetSetCookie

Sets a cookie on the specified URL.

Setting a Cookie

InternetSetCookieEx

Same as InternetSetCookie, but includes a parameter for verifying that the cookie is from a third party.

Setting a Cookie

Unlike most of the WinInet functions, the cookie functions do not require a call to InternetOpen.

Getting a Cookie

The following example demonstrates a call to InternetGetCookie.

Copy Code
char szURL[256]; 	// buffer to hold the URL
LPSTR lpszData = NULL; // buffer to hold the cookie data
DWORD dwSize=0; 	 // variable to get the buffer size needed
// Insert code to retrieve the URL.
retry:
// The first call to InternetGetCookie will get the required 
// buffer size needed to download the cookie data.
if (!InternetGetCookie(szURL, NULL, lpszData, &dwSize))
{
// Check for an insufficient buffer error.
	if (GetLastError()== ERROR_INSUFFICIENT_BUFFER)
	{   // Allocate the necessary buffer.
		lpszData = new char[dwSize];
		// Try the call again.
		goto retry;
}   else
	{// Insert error handling code. }
   }
else
{
	// Insert code to display the cookie data.
	// Release the memory allocated for the buffer.
	delete[]lpszData;
}

Setting a Cookie

You can use either InternetSetCookieor InternetSetCookieExto set a cookie on a specified URL. InternetSetCookiecan create both persistent and session cookies.

Persistent cookies are cookies that have an expiration date. These cookies are stored in the Windows\System directory. Session cookies are stored in memory and can be accessed only by the process that created them.

The data for the cookie should be in the format:

Copy Code
NAME=VALUE

For the expiration date, the format must be:

Copy Code
DAY, DD-MMM-YYYY HH:MM:SS GMT

DAY is the three-letter abbreviation for the day of the week, DD is the day of the month, MMM is the three-letter abbreviation for the month, YYYY is the year, and HH:MM:SS is the time of the day on a twenty-four-hour clock.

The following example shows two calls to InternetSetCookie. The first call creates a session cookie and the second creates a persistent cookie.

Copy Code
BOOL bReturn;
// Create a session cookie.
bReturn = InternetSetCookie("http://www.adventure-works.com", NULL,
			"TestData = Test");
// Create a persistent cookie.
bReturn = InternetSetCookie("http://www.adventure-works.com", NULL,
			 "TestData = Test; expires = Sat, 01-Jan-2000 00:00:00
GMT");

See Also

Concepts

HTTP Sessions