11

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.

Cohen
  • 180
  • 2
  • 11
  • You have also wasted 2 paragraphs begging for help (which you already did implicitly by posting the question), but entirely forgot to mention what is going wrong. Compile time, run time, specific error messages? – GolezTrol Oct 04 '12 at 08:01
  • Have you tried to remove the extra `/` you have in `http://www.filestrum.com//?op=my_account` ? – TLama Oct 04 '12 at 08:01
  • There is no error message, nothing. I get page code into Memo but seems it didn't pass auth. Same username&password work from Firefox. http://www.filestrum.com/?op=my_account - my error but it's still same. Whatever I try - can't log in from code. – Cohen Oct 04 '12 at 08:07
  • Make sure you have all [`parameters needed`](http://i.imgur.com/8Bb5x.jpg) for a login POST request. I bet you're missing at least `op` (operation type ?). Theoretically, can't verify (no Delphi by hand), you might try to do just a POST request and specify the `redirect` URL to that one where you've had that extra slash (before question edit). – TLama Oct 04 '12 at 08:21
  • I did everything I knew but still result is same :( – Cohen Oct 04 '12 at 08:29
  • It must be some issue with cookies, don't know what else it could be. – Cohen Oct 04 '12 at 08:36

1 Answers1

9

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;
TLama
  • 75,147
  • 17
  • 214
  • 392
whosrdaddy
  • 11,720
  • 4
  • 50
  • 99
  • @TLama: the POST request issues a 302 so it is up the the client side to issue a new GET request – whosrdaddy Oct 04 '12 at 09:11
  • @BotenAnna, there's no another one :-) – TLama Oct 04 '12 at 09:18
  • Hehe, finally. I waste days into this and just go in circle without any idea. Your was pretty close to working version TLama but seems this one from whosrdaddy works. Strange to me too. Guys, thanks once more. Hugs – Cohen Oct 04 '12 at 09:20
  • 1
    @TLama: Now I understand your reaction, you just need to include an OnRedirect handler on IdHTTP, so this solution is not 100% correct – whosrdaddy Oct 04 '12 at 09:20