Delphi使用表单授权从Web下载文件

时间:2014-02-27 13:06:54

标签: delphi authorization indy

我需要通过授权从Web下载文件。 我使用Delphi和Indy组件。 当方法Get执行下载时,我收到错误“HTTP / 1.0 404 Not Found”。 这似乎说授权失败,但文件Response.html包含网页,类似于登录后出现的网页...

P.S。我使用Firefox Web开发工具获取授权表格。

function TForm1.Login: string;
var
    Request: TStringList;
begin
    Result := '';
    try
        Request := TStringList.Create;
        try
            Request.Add('backurl=%2Fnewupgrade%2Findex.php');
            Request.Add('AUTH_FORM=Y');
            Request.Add('TYPE=AUTH');
            Request.Add('USER_LOGIN=xxxx');
            Request.Add('USER_PASSWORD=XXXX');
            IdHTTP1.AllowCookies := True;
            IdHTTP1.HandleRedirects := True;
            IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
            IdHTTP1.Request.Connection := 'keep-alive';
            Result := IdHTTP1.Post('http://www.uniko.ru/newupgrade/index.php?login=yes', Request);
        finally
            Request.Free;
        end;
    except
        on E: Exception do ShowMessage(E.Message);
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
    Response: string;
    fs: TStreamWriter;
    MS: TMemoryStream;
begin
    Response := Login;
    fs := TStreamWriter.Create('C:\Response.html');
    fs.Write(Response);
    fs.Free;

    // Download file
    MS := TMemoryStream.Create;
    try
        IdHTTP1.Request.ContentType := 'application/force-download; name="Setup.EXE"';
        IdHTTP1.Get('http://www.uniko.ru/newupgrade/izyat/Setup.EXE', MS);
        MS.SaveToFile('C:\Setup.tmp');
    finally
        MS.Free;
    end
end;

2 个答案:

答案 0 :(得分:1)

HTTP响应代码404表示找不到请求的URL。您正在考虑401,这是服务器的身份验证请求。 TIdHTTP在内部处理401,但根据它要求的身份验证类型(Basic,NTLM,SSPI等),您可能需要在IdAuthentication...子句中添加各种uses个单元使Indy能够支持他们。

您的代码写入Response.html的唯一方法是Post()报告的HTTP响应代码200而不是401,但是会将其发送回登录页面。该站点使用的是webform身份验证,而不是HTTP身份验证。   为webform身份验证发布TStrings时,请勿对内容进行URL编码。 Post()为您内部处理。具体来说,这一行:

Request.Add('backurl=%2Fnewupgrade%2Findex.php');

应改为:

Request.Add('backurl=/newupgrade/index.php');

这可能导致webform失败,并且可能会考虑404.查看发回的HTML,它可能包含错误消息。

答案 1 :(得分:0)

我找到了解决方案!!!

我读过这篇文章:http://www.programmersclub.ru/%D0%90%D0%B2%D1%82%D0%BE%D1%80%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8F-%D0%BD%D0%B0-mail-ru-%D1%81-%D0%BF%D0%BE%D0%BC%D0%BE%D1%89%D1%8C%D1%8E-indy/

并决定将Cooke参数添加到HTTP1.Response.RawHeaders:

procedure TForm1.Button1Click(Sender: TObject);
var
  header, cookie: string;
  MS: TMemoryStream;
  p, p2: Integer;
begin
  Login;

  cookie := '';
  header := IdHTTP1.Response.RawHeaders.Text;
  while true  do begin
    p := pos('Cookie:', header);
    if p=0 then break;
    p := p+length('Cookie:')+1;
    p2 := pos(#$D#$A, copy(header, p, length(header)));
    if p2=0 then p2 := length(header);
    cookie := cookie + copy(header, p, p2-1)+';';
    header := copy(header, p+p2+1, length(header))
  end;

  if cookie<>'' then begin
    cookie := StringReplace(cookie, 'path=/;', '', [rfReplaceAll]);
    cookie := copy(cookie, 1, length(cookie)-1);
    IdHTTP1.Request.CustomHeaders.Add('Cookie: ' + cookie);
  end;

  // Download file
  MS := TMemoryStream.Create;
  try
    IdHTTP1.Request.ContentType := 'application/force-download; name="Setup.EXE"';
    IdHTTP1.Get('http://www.uniko.ru/newupgrade/izyat/Setup.EXE', MS);
    MS.SaveToFile('C:\Setup.tmp');
  finally
    MS.Free;
  end
end;

它的工作!