PDA

Ver la Versión Completa : ¿Descargar Archivos de Sitios con Contraseña?


JXJ
01-10-2007, 23:21:11
¿como puedo descargar archivos de sitios web que requieren
de un usuario y una contraseña?

Ahora estoy usando WinInet y todo va bien mientras no use
contraseñas o autentificacion el sitio web. para acceder al archivo.
y con estos codigos

Esta funcion hace la descarga


procedure DownloadURLStream(const Url: string; Dest: TStream);
var
NetHandle: HINTERNET;
UrlHandle: HINTERNET;
Buffer: array[0..1024] of Char;
BytesRead: dWord;
begin
if not assigned(Dest) then exit;
Dest.Size := 0;
NetHandle := InternetOpen('Delphi', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(NetHandle) then begin
UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(UrlHandle) then begin
// UrlHandle valid? Proceed with download
FillChar(Buffer, SizeOf(Buffer), 0);
repeat
InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
if BytesRead > 0 then
Dest.Write(Buffer, BytesRead);
until BytesRead = 0;
InternetCloseHandle(UrlHandle);
end else
// UrlHandle is not valid. Raise an exception.
raise Exception.CreateFmt('Cannot open URL %s', [Url]);
InternetCloseHandle(NetHandle);
end else
// NetHandle is not valid. Raise an exception
raise Exception.Create('Unable to initialize Wininet');
end;


Esto esta en un boton

procedure TfrmMain.acLoadFromURLExecute(Sender: TObject);
var
URL: string;
M: TMemoryStream;
begin
// Ask user for URL
URL := InputBox('Which URL to download?', 'URL:', '');
// Setup memory stream
M := TMemoryStream.Create;
try
// Download URL to memory stream
DownloadURLStream(URL, M);
// Load from this stream
FXmlDoc.LoadFromStream(M);
// Regenerate views
Regenerate;
finally
M.Free;
end;
end;


gracias. :D