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
| procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
loginurl:='SiteA';
Params := TStringList.Create;
try
cookies:=tstringlist.Create;
html:=idhttp1.Get(loginurl);// Premier(s) cookie(s)
count:=IdCookieManager1.CookieCollection.count;
for i:=1 to count do
begin
cookies.Add(IdCookieManager1.CookieCollection.Items[i-1].CookieText);
showmessage(IdCookieManager1.CookieCollection.Items[i-1].CookieText);
end;
html:=idhttp1.Get(loginUrl); // pour avoir VIEWSTATE
poz:=pos('name="__VIEWSTATE" value="',html);
delete(html,1,poz+25);
viewstate:=copy(html,1,pos('"',html)-1);
showmessage(IntToStr(poz));
showmessage(viewstate);
Params.Values['__VIEWSTATE'] := viewstate;
Params.Values['TxtUsername'] := 'tarowras';
Params.Values['TxtPassword'] := '12345';
Params.Values['BtnLogin'] := 'Login';
setCookies;
IdHTTP1.HandleRedirects:=false;
try
HTML := IdHTTP1.Post(loginurl, Params);
except on e: EIdHTTPProtocolException do
begin
if e.ReplyErrorCode<>302 then
raise e;
count:=IdCookieManager1.CookieCollection.count; // Pour le UserID et Password
for i:=1 to count do
begin
cookies.Add(IdCookieManager1.CookieCollection.Items[i-1].CookieText);
showmessage('Etape 2 : '+IdCookieManager1.CookieCollection.Items[i-1].CookieText);
end;
setcookies;
html:=idhttp1.Get(IdHTTP1.Response.Location);// follow redirect
memo1.Lines.add(IdHTTP1.Get('SiteB'));
end;
end;
Params.Free;
end; |
Partager