本文整理自网络,侵删。
procedure TForm1.Button1Click(Sender: TObject);varHTTP: TidHTTP;html, s: string;i: integer;beginHTTP := TidHTTP.Create(nil);tryHTTP.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 thenfor i := 0 to HTTP.CookieManager.CookieCollection.Count - 1 dos := s + HTTP.CookieManager.CookieCollection.Items[i].CookieText;Memo1.Lines.Add(s);//取得CookiefinallyFreeAndNil(HTTP);end;end;//------------------------------------unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, IdCookieManager, IdBaseComponent, IdComponent, IdTCPConnection,IdTCPClient, IdHTTP;typeTForm1 = class(TForm)IdHTTP1: TIdHTTP;IdCookieManager1: TIdCookieManager;procedure FormCreate(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);varParams: TStringList;HTML, loginurl, myuser: String;count,i:integer;_cookies, cookies:tstringlist;ll:boolean;name,value:String;procedure setcookies;var j:integer; s:string;begincount:=cookies.count;s:='';for j:=1 to count dobeginIdCookieManager1.AddCookie(cookies[j-1],IdHTTP1.url.Host);s:=s+'; '+cookies[j-1];end;if s<>'' thenbegindelete(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;begini:=pos('=',cookie);k:=pos(';',cookie);if k=0 then k:=length(cookie);if i>0 thenbeginname:=copy(cookie,1,i-1);value:=copy(cookie,i+1,k-i-1);end elsebeginname:='';value:='';end;end;procedure savecookies;var j:integer;begincount:=IdCookieManager1.CookieCollection.count;for j:=1 to count dobeginextractcookie(IdCookieManager1.CookieCollection.Items[j-1].CookieText,name,value);cookies.Values[name]:=value;end;// IdCookieManager1.CookieCollection.Clear;end;procedure saveit(name:string);beginwith tfilestream.create(name,fmcreate) dotrywrite(pansichar(html)^,length(html));finallyfree;end;end;beginll:=false;loginurl:='http://feedmelinks.com/login';Params := TStringList.Create;trycookies:=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 viewstatesavecookies;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 redirecttrysetcookies;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 dobegin// 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 thenbeginsetCookies;html:=idhttp1.Get('http://feedmelinks.com/'); // software redirectsavecookies;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 dobeginif e.ReplyErrorCode<>302 thenraise e;// now this is the redirectcount:=IdCookieManager1.CookieCollection.count;// get the next cookie (this will be the userid)for i:=1 to count docookies.Add(IdCookieManager1.CookieCollection.Items[i-1].CookieText);setcookies;html:=idhttp1.Get(IdHTTP1.Response.Location);// follow redirectend;end;cookies.free;except on e: EIdHTTPProtocolException dobeginshowmessage(idHTTP1.response.ResponseText);end;end;Params.Free;showmessage('logged in? : '+booltostr(ll,true));end;end.
相关阅读 >>
Delphi recodedatetime、recodedate、recodetime、recodeyear ... 修改时间
Delphi debug模式下可以但是release模式下报错原因之一
Delphi tpath.combine(); {结合路径}
更多相关阅读请进入《Delphi》频道 >>