Entwickler-Ecke

Internet / Netzwerk - Kann mir jemand mit "web page login" helfen?


arcull - Mi 05.04.06 07:10
Titel: Kann mir jemand mit "web page login" helfen?
Ich habe die folgenede Kode aus dem Internet gezogen, um "internet programming" zu lernen. Alles get OK bis der Zeile "html:=idhttp1.Get(loginUrl)", da bekomme ich ein Error der sagt etwas uber "Raise Exepction...." Was habe ich falsch gemacht? Warum funktioniert Methode "Get" in zweitem Fall nicht? Hat jemand eine Idee?


Delphi-Quelltext
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:
var
 Params: TStringList;
 HTML, viewstate, loginurl: String;
 count,i,poz:integer;
 cookies:tstringlist;

 procedure setcookies;
 var j:integer;
 begin
   count:=IdCookieManager1.CookieCollection.count;
   for j:=1 to count do
     IdHTTP1.Request.RawHeaders.Add('Cookie'+IdHTTP1.Request.RawHeaders.NameValueSeparator+IdCookieManager1.CookieCollection.Items[j-1].CookieText);
 end;

begin
 loginurl:='http://www.geocaching.com/login/default.aspx?RESET=Y&redir=http%3a%2f%2fwww.geocaching.com%2fDefault.aspx';
 Params := TStringList.Create;
 try
   cookies:=tstringlist.Create;

   html:=idhttp1.Get('http://www.geocaching.com');// first get; get first cookie(s)
   count:=IdCookieManager1.CookieCollection.count;
   for i:=1 to count do
     cookies.Add(IdCookieManager1.CookieCollection.Items[i-1].CookieText);

   html:=idhttp1.Get(loginUrl);// next get; this is clean: used for retrieving the viewstate

   poz:=pos('name="__VIEWSTATE" value="',html);
   delete(html,1,poz+25);
   viewstate:=copy(html,1,pos('"',html)-1);
   Params.Values['__VIEWSTATE'] := viewstate;
   Params.Values['myUsername'] := [put your user name here];
   Params.Values['myPassword'] := [put your password here];
   Params.Values['Button1'] := 'Login';
   Params.Values['cookie'] := 'ON';

   setCookies;
   IdHTTP1.HandleRedirects:=false;// now this made the buzz, because the cookies were not set when following the redirect
   try
     HTML := IdHTTP1.Post(loginurl, Params);// now do the log in
   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(pos('log out',html)>0,true));
end;


Moderiert von user profile iconraziel: Delphi-Tags hinzugefügt