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 禁用本地网络连接/启用本地网络连接

Delphi dbgrid适应宽度

Delphi下载指定网址(url)的文件,带进度条显示

Delphi 多种方法查找窗口句柄

Delphi在case语句中使用字符串

Delphi 关于xe10下indy发送字符串编码的问题

Delphi写的dll回调c#

Delphi formatdatetime 显示日期时间

Delphi 获取百度ai 获取token

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



打赏

取消

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

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

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

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

评论

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