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;

相关阅读 >>

Delphi 根据快捷方式路径取源文件地址

Delphi 之 编辑框控件(tedit)

Delphi tstrings类的一些技巧

Delphi memo 字符串换行

如何在 Delphi 中静态链接 sqlite

Delphi 反外挂,反破解思想代码

Delphi 路径有空格调用批处理

Delphi xe6 读取android设备联系人

Delphi整理二(object pascal语言)

Delphi opentextfiledialog用法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...