如何使用带有 Delphi XE5 的 Indy 库登录和下载网站中的文件

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

我想登录一个网站,因为我需要下载一个文件。问题是我正在从网上下载 HTML 文件,我认为那是因为我无法登录。我只做了一次,但我不知道它是如何工作的以及为什么工作。 我不能告诉你我的用户名和密码,因为我用它来工作。

这是来源

  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;

IdHTTP.Get('https://www.example.com.ar/Catalog/DownloadFile/28', MS);URLDownloadToFile(nil, PChar('https://www.example.com.ar/Catalog/DownloadFile/28'),PChar('C:\mariano\files\urldownload.txt'), 0, nil); 下载 HTML 文件,我需要 link.

中的 .txt 文件

我知道我需要填写表格中的所有字段
在 HTML 中,表格看起来像这样...

<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>

我试了很多东西,我只需要登录并下载文件。我不知道我做错了什么。

Post()<form> 输入错误 URL。您需要将其 post 改为 https://www.example.com.ar/Account/Login,这由 <form>.

action 属性决定

此外,<form> 中没有定义 redirect 输入字段,所以您根本不应该发送它。

大多数现代网络浏览器都有内置的调试器,因此您应该能够先使用浏览器登录和下载文件,捕获发送的确切 HTTP 请求以及发送到的位置。然后您可以使用 TIdHTTP(或任何其他 HTTP 库)

复制相同的行为