Board index » cppbuilder » put_cookie does not set cookie

put_cookie does not set cookie


2004-01-28 07:39:08 PM
cppbuilder65
Hi
I;ve loaded a simple html doc using the Navigate2 method of TCppWebBrowser.
This displays fine. After loading the doc I try to set the cookie value
using put_cookie which SUCCEEDS but when I read the cookie value again with
get_cookie I get NULL???
I'm using XP and IE6.
void __fastcall TForm1::Button4Click(TObject *Sender)
{
AnsiString res;
try
{
AddCookie=true;
CookieName="Hello"; CookieValue="World; path=/;
domain=localhost;";
wb->Navigate2(&(TVariant)AnsiString("http://localhost/test/info.htm"),VT_EMP
TY,VT_EMPTY,VT_EMPTY,VT_EMPTY);
}
catch(const Exception &e)
{
ShowMessage(e.Message);
}
}
void __fastcall TForm1::wbDocumentComplete(TObject *Sender,
LPDISPATCH pDisp, TVariant *URL)
{
if (AddCookie)
{
AddCookie=false;
ChangeDocumentProperties(CookieName+"="+CookieValue,
wb->Document);
}
}
//--------------------------------------------------------------------------
-
void TForm1::ChangeDocumentProperties(AnsiString cookie, IDispatch* pDisp)
{
IHTMLDocument2* pHTMLDocument2=(IHTMLDocument2*)pDisp;
BSTR h;
AnsiString cs;
BSTR bstr_cookie = SysAllocString(WideString(cookie.c_str()).Detach());
if ( FAILED(pHTMLDocument2->put_cookie( bstr_cookie )) )
ShowMessage("Could not set Cookie!");
if (FAILED(pHTMLDocument2->get_cookie(&h))) ShowMessage("Unable to get
cookie");
cs=h;
SysFreeString(bstr_cookie);
}
In this case cs is ALWAYS NULL! Changing CookieValue to just "World;" does
not work either. Cookies are enabled as well as session cookies in IE6
Regards
Tim
 
 

Re:put_cookie does not set cookie

"Tim Chemaly" < XXXX@XXXXX.COM >wrote in message
Quote

wb->Navigate2(&(TVariant)AnsiString("http://localhost/test/info.htm"),VT_EMP
TY,VT_EMPTY,VT_EMPTY,VT_EMPTY);
If you are going to pass empty parameters, then don't pass them at all, they
are already set to empty by default:
wb->Navigate2(&TVariant("http://localhost/test/info.htm"));
Quote
IHTMLDocument2* pHTMLDocument2=(IHTMLDocument2*)pDisp;
That is not the correct way to cast the IDispatch. You must always use the
QueryInterface() method instead:
IHTMLDocument2* pHTMLDocument2 = NULL;
pDisp->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&pHTMLDocument2);
Quote
BSTR bstr_cookie = SysAllocString(WideString(cookie.c_str()).Detach());
You have a memory leak. Detach() unhooks the BSTR from the WideString,
making it the caller's responsibility to take ownership of it. But you are
not actually doing that, so the BSTR never gets freed. You are using the
BSTR to allocate a second BSTR and then you are working with that one
instead. Either don't call Detech(), or don't call SysAllocString():
bstr_cookie = SysAllocString(WideString(cookie));
or
bstr_cookie = WideString(cookie).Detach();
Or don't use a raw BSTR to begin with, but use a WideString variable
instead:
WideString bstrCookie = cookie;
pHTMLDocument2->put_cookie( bstrCookie )
Or simply:
pHTMLDocument2->put_cookie( WideString(cookie) )
Quote
if (FAILED(pHTMLDocument2->get_cookie(&h))) ShowMessage("Unable to get
cookie");
cs=h;
You have another memory leak, as you are never freeing the BSTR that
get_cookie() returns. Either call SysFreeString() or use a WideString
instead:
pHTMLDocument2->get_cookie(&h)
SysFreeString(h);
Or
WideString h;
pHTMLDocument2->get_cookie(&h)
Gambit
 

Re:put_cookie does not set cookie

Hi Remy
I made the changes as you suggested but I still can't set the cookie. Is it
possible though to set a cookie from a page that was simply loaded into the
browser memory?
void TForm1::ChangeDocumentProperties(AnsiString cookie, IDispatch* pDisp)
{
IHTMLDocument2* pHTMLDocument2=NULL;
if (FAILED(pDisp->QueryInterface(IID_IHTMLDocument2,
(LPVOID*)&pHTMLDocument2)))
{
ShowMessage("Could not create doc");
return;
}
BSTR h;
AnsiString cs;
if ( FAILED(pHTMLDocument2->put_cookie( WideString(cookie) )) )
ShowMessage("Could not set Cookie!");
if (FAILED(pHTMLDocument2->get_cookie(&h))) ShowMessage("Unable to get
cookie");
cs=h;
SysFreeString(h);
}
cs is still NULL!!
Bye
Tim
 

{smallsort}

Re:put_cookie does not set cookie

"Tim Chemaly" < XXXX@XXXXX.COM >wrote in message
Quote
I made the changes as you suggested but I still can't set the cookie.
That is because they weren't addressing that particular issue. They were
just general comments.
Quote
Is it possible though to set a cookie from a page that was
simply loaded into the browser memory?
It worked fine for me when I tested it just now, but only if the loaded
document saved its own cookies to begin with.
Gambit