将认证从Webbrowser转移到Indy CookieManager [英] Transfer Authentication from Webbrowser to Indy CookieManager

查看:878
本文介绍了将认证从Webbrowser转移到Indy CookieManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Web浏览器中的cookie放入用于Http请求的Indy CookieManager。

How do you put the cookie from the Webbrowser to an Indy CookieManager for Http Request.

我登录到这样一个网站后,我得到了cookies。

I get the cookies after i login in to a website like this..

测试项目= http://www.megafileupload.com/en/file/373536/Cookie-Tester-rar.html

procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
var
document: IHTMLDocument2;
cookies:tstringlist;
begin
cookies:=tstringlist.Create;
document := WebBrowser1.Document as IHTMLDocument2;
cookies.Add(document.cookie);
cookies.savetofile('test.txt');
end;

HttpOnly Cookie方法。

function GetCookie(host: PAnsiChar): PAnsiChar;
const
  INTERNET_COOKIE_HTTPONLY = 8192;
var
  hModule: THandle;
  lp: Pointer;
  InternetGetCookieEx: function(lpszUrl, lpszCookieName, lpszCookieData
    : PAnsiChar; var lpdwSize: DWORD; dwFlags: DWORD; lpReserved: pointer)
    : BOOL; stdCall;
  CookieSize: DWORD;
  CookieData: PAnsiChar;
begin
  LoadLibrary('wininet.dll');
  hModule := GetModuleHandle('wininet.dll');
  if hModule <> 0 then
  begin
    @InternetGetCookieEx := GetProcAddress(hModule, 'InternetGetCookieExA');
    if @InternetGetCookieEx <> nil then
    begin
      CookieSize := 1024;
      Cookiedata := AllocMem(CookieSize);
      if InternetGetCookieEx(host, nil, Cookiedata, CookieSize, INTERNET_COOKIE_HTTPONLY, nil) then
      result:=cookiedata;
      FreeMem(Cookiedata);
    end;
  end;
end;


推荐答案

由于您使用多个Delphi版本标签标记了您的问题,我假设你正在使用不同版本的Indy与每个Delphi版本,是对的吗? Indy的cookie处理逻辑多年来发生了一些变化,2011年初进行了重新修改,以说明 RFC 6265 (其中已废弃所有以前的cookie RFC)。

Since you tagged your question with multiple Delphi version tags, I assume you are using different releases of Indy with each Delphi version, is that right? Indy's cookie handling logic has changed a bit over the years, and underwent a major re-wrote in early 2011 to account for RFC 6265 (which obsoleted all previous cookie RFCs).

在当前的Indy 10版本中,手动添加cookies是使用 TIdCookieManager.AddServerCookie() TIdCookieManager.AddServerCookies()方法:

Under the current Indy 10 release, adding cookies manually is done using the TIdCookieManager.AddServerCookie() or TIdCookieManager.AddServerCookies() method:

procedure AddServerCookie(const ACookie: String; AURL: TIdURI);
procedure AddServerCookies(const ACookies: TStrings; AURL: TIdURI);

这两个参数都是必需的,其中 ACookie 一个 name = value;参数单个Cookie的字符串, AURL 是Cookie来自的URL(用于验证cookie数据并初始化任何默认值,需要),例如:

Both parameters are required, where ACookie is a name=value; parameters string for a single cookie, and AURL is the URL where the cookie came from (used for validating the cookie data and initializing any default values where needed), for example:

procedure TForm1.WebBrowser1DownloadComplete(Sender: TObject);
var
  document: IHTMLDocument2;
  cookies: TStringList;
  uri: TIdURI;
begin
  document := WebBrowser1.Document as IHTMLDocument2;
  cookies := TStringList.Create;
  try
    // fill cookies as needed, one cookie per line
    uri := TIdURI.Create(document.URL);
    try
      IdCookieManager1.AddServerCookies(cookies, uri);
    finally
      uri.Free;
    end;
  finally
    cookies.Free;
  end;
end;

请记住, document.cookie 属性可以包含多个Cookie,因此您必须手动拆分Cookie,然后才能将其传递到 TIdCookieManager 。此外, document.cookie 属性使用; 字符来分隔cookie,但它也使用';'分隔单个cookie的 name = value 参数值,所以你将要做一点点在分解 document.cookie 数据时进行解析。

Keep in mind that the document.cookie property can contain multiple cookies in it, so you will have to split the cookies up manually before you can then pass them to TIdCookieManager. Also, the document.cookie property uses the ; character to separate cookies, but it also uses ';' for separating the name=value and parameters values for a single cookie, so you are going to have to do a little bit of parsing when splitting up the document.cookie data.

这篇关于将认证从Webbrowser转移到Indy CookieManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆