Delphi - logging in to webpage from code

2019-06-14 08:49发布

问题:

I am using Delphi 7 and require some help with a problem, and yes, I have searched everywhere for an answer, yet the supplied code isn't documented , so I have no idea how it works or how to adjust it to fit my needs.

What I am trying to achieve is to log in to the website http://kissanime.com/login ..

The problem I'm experiencing is that I need to stay logged in while my program does some work on that website's HTML ( Basically I want to parse out the download links of a certain series, but I need to be logged in to view said links )...

I have no code that I can supply as none of the code that I saw while searching for a solution didn't make much sense, however, I have an idea of what needs to be done. I think that using some POST method to the website to supply it with the username and password would be a start, from there on out, I'm not sure whether or not I'd stay logged in.

I may be overcomplicating this problem and there is perhaps a simple way of achieving this, and that is why I have turned to the one site that I usually get all my answers from, I hope that I explained the problem well enough and that I can find some help..

I Do not expect source code , since I did not supply any here, but even some useful links that could explain this process to me would be much appreciated.

Here I have created a simple collage of images to just solidify my explanation of the problem :

Thank you for your time and I hope that I can find some clarity on this subject!

回答1:

You need to first Get() the webpage that contains the webform and let it generate whatever cookie(s) it needs, and let TIdHTTP capture it/them (if you do not attach a TIdCookieManager to the TIdHTTP.CookieManager property, TIdHTTP creates a TIdCookieManager internally), then you can submit the desired webform data to the appropriate webpage and TIdHTTP will include any relevant cookie(s) it is currently holding. For example:

// get cookies first...
IdHTTP1.Get('http://kissanime.com/login');

// now submit the webform...
Params := TStringList.Create;
try
  Params.Add('username=' + UserName);
  Params.Add('password=' + Password);
  Params.Add('chkRemember=1'); // <-- omit this if you do not want to enable the webform's "Remember me" option
  Params.Add('btnSubmit=');
  Params.Add('redirect=');

  IdHTTP1.Post('http://kissanime.com/login', Params);
finally
  Params.Free;
end;