Delphi Cookie获取及使用

以下方法为网上搜集整理,留做备份,随时更新

 

一:通过URL获取

CanGetIECookie(URL,g_cookie);

function   CanGetIECookie(const   URL:   string;   var   Cookie:   string):   boolean;
  var
      lpvBuffer:   array[0..1000]   of   byte;
      lpdwBufferLength:   cardinal;
  begin
      lpdwBufferLength   :=   sizeof(lpvBuffer);
      result   :=   InternetGetCookie(PChar(URL),   nil,   @lpvBuffer,   lpdwBufferLength);
      if   result   then
          Cookie   :=   pchar(@lpvBuffer);
  end;

 

二:从响应报文中获取

idc := TIdHTTP.Create(nil);

for i := 0 to idc.Response.RawHeaders.Count - 1 do
      begin
        tmp := Idc.Response.RawHeaders[i];
        if pos('cookie:', LowerCase(tmp)) = 0 then
          Continue;

        tmp := Trim(Copy(tmp, Pos('Cookie:', tmp) + Length('Cookie:'), Length(tmp)));

        if cookie = '' then
          cookie := tmp
        else
          cookie := cookie + '; ' + tmp;
      end;

 

三:通过TWebBrowser 控件获取

WB_logon: TWebBrowser

cookie := WB_logon.OleObject.document.cookie;

 

将cookie添加至请求中

var

  idc : TIDHTTP;

begin

  idc := TIdHTTP.Create(nil);

  idc.Request.CustomHeaders.Add('cookie:' + g_cookie);

end;

 

posted @ 2016-07-08 00:05  且随风  阅读(3870)  评论(0编辑  收藏  举报