idHTTP最简洁的修改和取得Cookie例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | procedure TForm1 . Button1Click(Sender: TObject); var HTTP: TidHTTP; html, s: string ; i: integer ; begin HTTP := TidHTTP . Create( nil ); try HTTP . HandleRedirects := True ; HTTP . AllowCookies := True ; HTTP . Request . CustomHeaders . Values[ 'Cookie' ] := 'abcd' ; //修改Cookie 抓包可见 html := HTTP . Get( 'http://www.baidu.com/' ); s := 'Cookies: ' ; if HTTP . CookieManager . CookieCollection . Count > 0 then for i := 0 to HTTP . CookieManager . CookieCollection . Count - 1 do s := s + HTTP . CookieManager . CookieCollection . Items[i].CookieText; Memo1 . Lines . Add(s); //取得Cookie finally FreeAndNil(HTTP); end ; end ; //------------------------------------ unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdCookieManager, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP; type TForm1 = class (TForm) IdHTTP1: TIdHTTP; IdCookieManager1: TIdCookieManager; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end ; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1 . FormCreate(Sender: TObject); var Params: TStringList; HTML, loginurl, myuser: String ; count,i: integer ; _cookies, cookies:tstringlist; ll: boolean ; name,value: String ; procedure setcookies; var j: integer ; s: string ; begin count:=cookies . count; s:= '' ; for j:= 1 to count do begin IdCookieManager1 . AddCookie(cookies[j- 1 ],IdHTTP1 . url . Host); s:=s+ '; ' +cookies[j- 1 ]; end ; if s<> '' then begin delete(s, 1 , 2 ); s:=s+ ';' ; IdHTTP1 . Request . CustomHeaders . Values[ 'Cookie' ]:=s; IdHTTP1 . Request . RawHeaders . Values[ 'Cookie' ]:=s; //('Cookie'+IdHTTP1.Request.RawHeaders.NameValueSeparator+s); end ; {} end ; procedure extractcookie(cookie: string ; var name,value: string ); var i,k: integer ; begin i:=pos( '=' ,cookie); k:=pos( ';' ,cookie); if k= 0 then k:=length(cookie); if i> 0 then begin name:=copy(cookie, 1 ,i- 1 ); value:=copy(cookie,i+ 1 ,k-i- 1 ); end else begin name:= '' ; value:= '' ; end ; end ; procedure savecookies; var j: integer ; begin count:=IdCookieManager1 . CookieCollection . count; for j:= 1 to count do begin extractcookie(IdCookieManager1 . CookieCollection . Items[j- 1 ].CookieText,name,value); cookies . Values[name]:=value; end ; // IdCookieManager1.CookieCollection.Clear; end ; procedure saveit(name: string ); begin with tfilestream . create(name,fmcreate) do try write ( pansichar (html)^,length(html)); finally free; end ; end ; begin ll:= false ; loginurl:= 'http://feedmelinks.com/login' ; Params := TStringList . Create; try cookies:=tstringlist . Create; // cookies.Duplicates:=dupIgnore; // cookies.Sorted:=true; idhttp1 . Host:= 'feedmelinks.com' ; html:=idhttp1 . Get( 'http://feedmelinks.com/' );// first get; get first cookie(s) savecookies; setcookies; html:=idhttp1 . Get(loginUrl); // next get; this is clean: used for retrieving the viewstate savecookies; myuser:= 'crystyignat' ; Params . Values[ 'userId' ] := myuser; Params . Values[ 'password' ] := 'mypassword' ; Params . Values[ 'op' ] := 'login' ; IdHTTP1 . HandleRedirects:= false ; // now this made the buzz, because the cookies were not set when following the redirect try setcookies; HTML := IdHTTP1 . Post(loginurl, Params); // now do the log in _Cookies := TStringList . Create; IdHTTP1 . Response . RawHeaders . Extract( 'Set-cookie' , _Cookies); for i := 0 to _Cookies . Count - 1 do begin // IdCookieManager1.AddCookie(_Cookies[i], IdHTTP1.URL.Host); extractcookie(_Cookies[i],name,value); cookies . Values[name]:=value; end ; _cookies . free; // savecookies; if pos( '<div class="welcome">Welcome, <b>' +myuser+ '</b>' ,html)> 0 then begin setCookies; html:=idhttp1 . Get( 'http://feedmelinks.com/' ); // software redirect savecookies; saveit( 'hhh.html' ); // setCookies; // html:=idhttp1.Get('http://feedmelinks.com/portal'); // another software redirect //savecookies; ll:=pos( '<a class="tn" href="logout">log out' ,html)> 0 ; end ; except on e: EIdHTTPProtocolException do begin if e . ReplyErrorCode<> 302 then raise e; // now this is the redirect count:=IdCookieManager1 . CookieCollection . count; // get the next cookie (this will be the userid) for i:= 1 to count do cookies . Add(IdCookieManager1 . CookieCollection . Items[i- 1 ].CookieText); setcookies; html:=idhttp1 . Get(IdHTTP1 . Response . Location); // follow redirect end ; end ; cookies . free; except on e: EIdHTTPProtocolException do begin showmessage(idHTTP1 . response . ResponseText); end ; end ; Params . Free; showmessage( 'logged in? : ' +booltostr(ll, true )); end ; end . |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库