locked
IXMLHTTPRequest2 cookie

    Question

  • Hi

    Anyone has sample code to set/get cookie for IXMLHTTPRequest2?


    rob qqq

    Friday, September 28, 2012 12:20 AM

Answers

  • You can use the following approach to setting the cookie information:

     XHR_COOKIE testCookie;
     testCookie.pwszUrl =  (WCHAR *)uri;          // where uri = http://www.YourServer.com/yourDirectory/yourpath...
     testCookie.pwszName = L"testCookieName";     // the cookie name
     testCookie.pwszValue = L"testCookieValue";   // the cookie value
     testCookie.pwszP3PPolicy = NULL;             // any
     testCookie.dwFlags = XHR_COOKIE_HTTPONLY | XHR_COOKIE_IS_SESSION; // you can retrieve the flag information from the msxml6.h header file found inside: C:\Program Files (x86)\Windows Kits\8.0\Include\um. Look at the _XHR_COOKIE_FLAG enumeration for all the allowed values.
     
     FILETIME ft;
     ft.dwHighDateTime = 0;
     ft.dwLowDateTime = 0;
     testCookie.ftExpires = ft; // this is the Cookie expiration time. It is set to NULL since this is a session cookie.

     DWORD dwCookieState=0;
     HRESULT hrTest = xhr->SetCookie(&testCookie, &dwCookieState);

    Thanks,

    Prashant.

    Saturday, October 13, 2012 12:00 AM
    Moderator
  • Yes, the returned pointer to the cookie value is an array of XHR_COOKIE so you should start from the 0 index. You need to call GetCookie only once to retrieve the cookie information. Depending on the flags you provide (same flags used during SetCookie) as well as the pwszName (either give a specific name or give NULL to retrieve all the cookies) parameter you can retrieve all the cookies for that specific request/ URL.

    Hope that helps.

    Thanks,

    Prashant.

    • Marked as answer by rob qqq Tuesday, October 16, 2012 10:24 PM
    Tuesday, October 16, 2012 8:51 PM
    Moderator

All replies

  • You can use the following approach to setting the cookie information:

     XHR_COOKIE testCookie;
     testCookie.pwszUrl =  (WCHAR *)uri;          // where uri = http://www.YourServer.com/yourDirectory/yourpath...
     testCookie.pwszName = L"testCookieName";     // the cookie name
     testCookie.pwszValue = L"testCookieValue";   // the cookie value
     testCookie.pwszP3PPolicy = NULL;             // any
     testCookie.dwFlags = XHR_COOKIE_HTTPONLY | XHR_COOKIE_IS_SESSION; // you can retrieve the flag information from the msxml6.h header file found inside: C:\Program Files (x86)\Windows Kits\8.0\Include\um. Look at the _XHR_COOKIE_FLAG enumeration for all the allowed values.
     
     FILETIME ft;
     ft.dwHighDateTime = 0;
     ft.dwLowDateTime = 0;
     testCookie.ftExpires = ft; // this is the Cookie expiration time. It is set to NULL since this is a session cookie.

     DWORD dwCookieState=0;
     HRESULT hrTest = xhr->SetCookie(&testCookie, &dwCookieState);

    Thanks,

    Prashant.

    Saturday, October 13, 2012 12:00 AM
    Moderator
  • testCookie.pwszUrl =  (WCHAR *)uri;   Is this uri the same as the one passed into IXHttpRequest2::Open method?  

    rob qqq

    Saturday, October 13, 2012 8:40 PM
  • Yes, that is correct.
    Monday, October 15, 2012 7:03 PM
    Moderator
  • Interesting, not sure whey do we need to pass uri again because we already passed it in open method. Lower layer should have uri saved in xhr object.

    Anyhow, How about pcCookies parameter in GetCookie API? Shall I start from 0 to n. And when to stop calling GetCookie?


    rob qqq

    Tuesday, October 16, 2012 8:24 PM
  • Yes, the returned pointer to the cookie value is an array of XHR_COOKIE so you should start from the 0 index. You need to call GetCookie only once to retrieve the cookie information. Depending on the flags you provide (same flags used during SetCookie) as well as the pwszName (either give a specific name or give NULL to retrieve all the cookies) parameter you can retrieve all the cookies for that specific request/ URL.

    Hope that helps.

    Thanks,

    Prashant.

    • Marked as answer by rob qqq Tuesday, October 16, 2012 10:24 PM
    Tuesday, October 16, 2012 8:51 PM
    Moderator
  • thanks. in other words, if I specify cookie name for get,  pcCookies should always be 0? I think one cookie name should only have one value. Right?

    rob qqq

    Tuesday, October 16, 2012 10:02 PM
  • It really depends. If you already know the name of the cookie and if the cookie exists in the collection, you will get that value. Otherwise if it doesn't exist you will get the value 0. If you specify NULL as the name, you should get back all the cookies present in the Set-Cookie header that are corresponding to the path (uri) parameter.

    The server can push multiple cookie values with the same name, but the Windows implementation of IXMLHTTPRequest2 interface only returns the last value that was present in the Set-Cookie header.

    Wednesday, October 17, 2012 6:10 PM
    Moderator