Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

indy - Log in to website from Delphi

I would ask if someone was kind enough to explain to me how to login at webpage from Delphi app. All the examples I've found here have proved useless to me or I'm doing something wrong. I'm tired of the search and the code that does not work.

There is no error message, I even get page code into Memo but seems it's code from login page (not account [dashboard] page) - seems this code can't pass auth at all and I don't know why.

What is wrong in this code :

procedure Login;
var
 HTTP: TIdHTTP;
 Param: TStringList;
 S: String;
begin
 HTTP := TIdHTTP.Create(nil);
 HTTP.CookieManager := Main_Form.CookieManager;
 Param := TStringList.Create;
 Param.Clear;
 Param.Add('login=example');
 Param.Add('password=example');

try
 HTTP.Get ('http://www.filestrum.com/login.html');
 HTTP.Post('http://www.filestrum.com/login.html', Param);
 S := HTTP.Get ('http://www.filestrum.com/?op=my_account');
 Main_Form.Memo2.Lines.Add(S);
finally
  HTTP.Free;
  Param.Free;
end;
end;

or with this version :

procedure Login;
var
 HTTP: TIdHTTP;
 S: String;
begin  
 HTTP                             := TIdHTTP.Create(nil);
 HTTP.CookieManager               := Main_Form.CookieManager;
 HTTP.Request.BasicAuthentication := True;
 HTTP.Request.Username            := 'example';
 HTTP.Request.Password            := 'example';
 HTTP.AllowCookies                := True;
 HTTP.HandleRedirects             := True;

 S := HTTP.Get ('http://www.filestrum.com/?op=my_account');
 Main_Form.Memo2.Lines.Add(S);
end;

Used Delphi XE2 and there is no way to make this code running and login. It's same with XE3 demo. As I said, I'm really tired searching some solution, waste days into it and nothing.

Please guys, some help here. Really need it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try something like this:

function Login: string;
var
  IdHTTP: TIdHTTP;
  Request: TStringList;
  Response: TMemoryStream;
begin
  Result := '';
  try
    Response := TMemoryStream.Create;
    try
      Request := TStringList.Create;
      try
        Request.Add('op=login');
        Request.Add('redirect=http://www.filestrum.com');
        Request.Add('login=example');
        Request.Add('password=example');
        IdHTTP := TIdHTTP.Create;
        try
          IdHTTP.AllowCookies := True;
          IdHTTP.HandleRedirects := True;
          IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
          IdHTTP.Post('http://www.filestrum.com/', Request, Response);
          Result := IdHTTP.Get('http://www.filestrum.com/?op=my_account');    
        finally
          IdHTTP.Free;
        end;
      finally
        Request.Free;
      end;
    finally
      Response.Free;
    end;
  except
    on E: Exception do
      ShowMessage(E.Message);
  end;
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...