Synapse to html login lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- xinyiman · 2015-04-09 19:29 Hello guys, I would like to figure out how to log in to my html page that has this code:
Login:
Pwd:
Url: www.xyz.com Obviously using lazarus and synapse. Help Me ---------------------------------------------------------------------- skalogryz · 2015-04-09 20:07 should it be as complicated as Code: [Select]function Login(const username, password: string; var loginResult: string): Boolean; var   data : TStringStream; begin   data := TStringStream.Create;   try     Result:=HttpPostURL( 'www.xyz.com/login.php'      , 'user_id='+EncodeURLElement(username)+'&user_pwd='+EncodeURLEle ment(password)      , data);     loginResult:=data.DataString;   finally     data.Free;   end; end; The example is taken from here. ---------------------------------------------------------------------- skalogryz · 2015-04-09 20:09 ah... you might also need to consider the protocol! So url should be Code: [Select]http://www.xyz.com/login.php or (more likely) Code: [Select]https://www.xyz.com/login.php in later case OpenSSL is required (unless using some other encryption library) ---------------------------------------------------------------------- skalogryz · 2015-04-09 20:12 Also!  Some sites are doing password hashing or some sort of re- encryption of password.  (as much as it could be done on Javascript side). For example this forum (forum.lazarus.freepascal.org) doesn't pass the password entered, instead if calculates its hash. So if that's the case for xyz.com, you want to make sure you handle the password accordingly ---------------------------------------------------------------------- xinyiman · 2015-04-09 20:31 Thanks for the answer, but I want to make access to the site to access the content. I can not figure out how to let him know that I have access credentials and then use the site on my own. Done log want to turn on my site as I want. As if I autentificassi site http://forum.lazarus.freepascal.org to check if I have new messages and alerts you when it does me so I'm going to see them from the browser. How can I do? ---------------------------------------------------------------------- rvk · 2015-04-09 21:16 After issuing the first HttpPostURL with username and password to the login-page you should be able to access other pages as well as if you were logged in. The problem is that you don't have access to the cookies set in HttpPostURL. So you have to use THTTPSend manually to post to the login-site and after that you can use the same instance of THTTPSend to retrieve other pages with the session intact. (You could also save the Cookie property but if you use the same instance of THTTPSend that isn't necessary) It should look something like this (I'm not able to test it myself here but you get an idea): Code: [Select]var   HTTPx: THTTPSend;   vrUser, vrPass: String; begin   vrUser := 'username';   vrPass := 'password';   HTTPx := THTTPSend.create;   try     HTTPx.MimeType := 'application/x-www-form-urlencoded';     WriteStrToStream(HTTPx.Document, ansistring('user_id=' + vrUser + '&user_pwd=' + vrPass));     if HTTPx.HTTPMethod('POST', 'http://www.xyz.com/login.php') then     begin       HTTPx.Clear; // clear everything EXCEPT COOKIES       if HTTPx.HTTPMethod('GET', 'http://www.xyz.com/some_other_page_like_messages.php') then       begin         // do something with HTTPx.Document       end;       HTTPx.Clear; // clear everything EXCEPT COOKIES       if HTTPx.HTTPMethod('GET', 'http://www.xyz.com/and_another_page.php') then       begin         // do something with the other page in HTTPx.Document       end;     end;    finally     HTTPx.Free;   end; end; (I use this method successfully in my own program) ---------------------------------------------------------------------- BeniBela · 2015-04-10 13:01 That is what my Internet Tools were made for: Code: [Select]uses simpleinternet; var temp IXQValue; begin   httpRequest(process('http://forum.lazarus.freepascal.org/index.php?a ction=login', 'form(css("#frmLogin"), {"user": "BeniBela", "passwrd": "xxxxxxxxxx"})');   for temp in process('http://forum.lazarus.freepascal.org/index.php?action=pm',    'css("#personal_messages tr")/td[3]') do     writeln(temp.toString) Should print the title of all messages (on Linux the httpRequest function will call Synapse...)