问题
Trying to download a file using indy,(post to asp save the excel response) but running into errors, using wireshark the request is missing cookies.
Trying to grab the cookie out of a Twebbrowser window and save it.
procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
var
document: IHTMLDocument2;
cookies:tstringlist;
begin
cookies:=tstringlist.Create;
document := WebBrowser1.Document as IHTMLDocument2;
cookies.Add(document.cookie);
//do stuff with them
end;
returns nothing, whats the best way to extract a cookie(or 2) out of twebbrowser, or is there something better i'm missing?
回答1:
TWebBrowser
is a wrapper around Internet Explorer, which itself is a wrapper around WinInet. Indy does not share cookies with IE/WinInet, so you have to copy the cookie details manually into Indy's TIdCookieManager
component. However, the IHTMLDocument2.cookie
property is just a delimited string of name=value
pairs (if it returns anything at all, due to security restrictions), which does not contain enough information for Indy's use, such as a cookie's source URL and target domain/path, so you have to get that information from somewhere else, such as by parsing IE's cookie files that are stored in Windows' Cookies
folder.
回答2:
Because of a history with malware (ab)using JavaScript and the document.cookie property, web-servers may request on the HTTP level that the cookie only be returned over HTTP and not be available over JavaScript.
https://www.owasp.org/index.php/HTTPOnly
I'm not sure this is the case in your instance, but you mention you use Indy to fetch a file. If you put the data from it into WebBrowser1 yourself, it would make sense the cookie data is not copied along, since it is a property of the HTTP transaction. See the Indy documentation about cookies:
http://www.indyproject.org/docsite/html/TIdHTTP_CookieManager.html
来源:https://stackoverflow.com/questions/14826727/getting-cookie-from-twebbrowser