[英]Delphi download file from Web with Form authorization
我需要授權從Web下載文件。 我使用Delphi和Indy組件。 當Get方法執行下載時,出現錯誤“ HTTP / 1.0 404 Not Found”。 這似乎表明授權失敗,但是文件Response.html包含網頁,類似於登錄后出現的網頁……
我使用Firefox Web開發工具獲得的PS Prams授權表。
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;
HTTP響應代碼404表示找不到請求的URL。 您正在考慮401,這是服務器的身份驗證請求。 TIdHTTP
內部處理401,但是根據其要求的身份驗證類型(基本,NTLM,SSPI等),您可能需要在uses
子句中添加各種IdAuthentication...
單元,以啟用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');
這可能會導致Web表單失敗,並可能導致404錯誤。看看發送回的HTML,其中可能包含錯誤消息。
我找到解決辦法!!!
我讀了這篇文章: 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-郵件rU-亞%D1%81-%D0%BF%D0%BE%D0%BC%D0%BE%D1%89%D1 %8C%D1%8E-印/
並確定將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;
和它的工作!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.