Ver Mensaje Individual
  #1  
Antiguo 01-10-2007
JXJ JXJ is offline
Miembro
 
Registrado: abr 2005
Posts: 2.475
Reputación: 22
JXJ Va por buen camino
¿Descargar Archivos de Sitios con Contraseña?

¿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

Código Delphi [-]
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
Código Delphi [-]
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.
Responder Con Cita