htmldelphi-xe5

How to Login and Download a File in a Website using Indy's Libraries with Delphi XE5


I want to login in a website cause I need to download a file. The problem is that I'm downloading the HTML file from the web, and I think that's because I can't log in. I just made it once, and I don't know how and why it worked. I can't show you my user and password because I use it for work.

Here's the source

  IdHTTP: TIdHTTP;
  IdSSL: TIdSSLIOHandlerSocketOpenSSL;
  Request: TStringList;
  MS: TMemoryStream;
begin

  try
    IdHTTP := TIdHTTP.Create;
  finally
  end;

  try
    IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
    IdHTTP.IOHandler := IdSSL;
    IdHTTP.AllowCookies := True;
    IdHTTP.HandleRedirects := True;
    IdHTTP.Get('https://www.example.com.ar');

    Request := TStringList.Create;
    try
      Request.Add('customer=myuser');
      Request.Add('Password=mypassword');
      Request.Add('User= ');
      Request.Add('redirect=https://www.example.com.ar/Catalog');
      IdHTTP.Post('https://www.example.com.ar', Request);
    finally
        Request.Free;
    end;

    MS := TMemoryStream.Create;

    try

// Here I use these two ways to download the file

      IdHTTP.Get('https://www.example.com.ar/Catalog/DownloadFile/28', MS);
      MS.SaveToFile('C:\mariano\files\httpGET.txt');

      URLDownloadToFile(nil, PChar('https://www.example.com.ar/Catalog/DownloadFile/28'),
      PChar('C:\mariano\files\urldownload.txt'), 0, nil);

    finally
      MS.Free;
    end;
  finally
    IdHTTP.Free;
    IdSSL.Free;
  end;
end;

Both, IdHTTP.Get('https://www.example.com.ar/Catalog/DownloadFile/28', MS); and URLDownloadToFile(nil, PChar('https://www.example.com.ar/Catalog/DownloadFile/28'),PChar('C:\mariano\files\urldownload.txt'), 0, nil); Downloads a HTML File, and I need the .txt file that is in that link.

I know that I need to fill al the fields in the form
And in the HTML the form looks like this...

<form action="/Account/Login" data-ajax="true" data-ajax-begin="dds.showLoading" data-ajax-complete="dds.Scope.configureUnobtrusiveValidations" data-ajax-failure="dds.Scope.Failure" data-ajax-method="Post" data-ajax-mode="replace" data-ajax-success="dds.Scope.logInComplete" data-ajax-update="#loginContainer" id="form0" method="post">    <div class="clearfix">
        <div class="sep">
            
            <input data-val="true" data-val-required="El número de cliente es requerido" id="customer" maxlength="10" name="Customer" placeholder="Usuario" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="Customer" data-valmsg-replace="true"></span>
        </div>
        <div class="sep">
            
            <input data-val="true" data-val-required="La contraseña es requerida" id="Password" name="Password" placeholder="Contraseña" type="password" />
            <span class="field-validation-valid" data-valmsg-for="Password" data-valmsg-replace="true"></span>
        </div>
        <div class="sep">
            
            <input id="User" name="User" placeholder="Función" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="User" data-valmsg-replace="true"></span>
        </div>
        <div class="btn">
            <input type="submit" value="Ingresar" />
        </div>
    </div>
<input data-val="true" data-val-number="The field ResolutionWidth must be a number." data-val-required="The ResolutionWidth field is required." id="ResolutionWidth" name="ResolutionWidth" type="hidden" value="" /><input data-val="true" data-val-number="The field ResolutionHeight must be a number." data-val-required="The ResolutionHeight field is required." id="ResolutionHeight" name="ResolutionHeight" type="hidden" value="" /></form>        </div>

I tried a lot of things, I just need to Login and Download the File. I don't know what I'm doing wrong.


Solution

  • You are Post()'ing the <form> input to the wrong URL. You need to post it to https://www.example.com.ar/Account/Login instead, as determined by the action attribute of the <form>.

    Also, there is no redirect input field defined in the <form>, so you should not be sending that at all.

    Most modern web browsers have a built-in debugger, so you should be able to login and download a file using a browser first, capturing the exact HTTP requests being sent and where they are being sent to. Then you can replicate that same behavior with TIdHTTP (or any other HTTP library)