DICAS

Visite a biblioteca de dicas da comunidade.

Saiba mais

ARTIGOS

Abordagens detalhadas sobre assuntos diversos.

Saiba mais

INICIANTES

Aprenda a programar de um modo simples e fácil.

Saiba mais

DOWNLOADS

Acesse os materiais exclusivos aos membros.

Saiba mais
voltar

PARA QUEM GOSTA DE DELPHI

Como salvar o código fonte de uma página em um arquivo?

Esta função salva o código fonte de uma página WEB indicada.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function DownloadArquivoByUrl(const AUrl, ADestino: String): Boolean;
const
  BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array [1 .. BufferSize] of Byte;
  BufferLen: DWORD;
  f: File;
  sAppName: string;
begin
  Result := False;
  sAppName := ExtractFileName(Application.ExeName);
  hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG,
    nil, nil, 0);
  try
    hURL := InternetOpenURL(hSession, PChar(AUrl), nil, 0, 0, 0);
    try
      if Assigned(hURL) then
      begin
        AssignFile(f, ADestino);
        Rewrite(f, 1);
        repeat
          InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
          BlockWrite(f, Buffer, BufferLen)
        until BufferLen = 0;
        CloseFile(f);
        Result := true;
      end;
    finally
      InternetCloseHandle(hURL);
    end
  finally
    InternetCloseHandle(hSession);
  end
end;

Exemplo de uso:

1
2
3
4
procedure TFrmBitMain.Button1Click(Sender: TObject);
begin
  DownloadArquivoByUrl('http://globoesporte.globo.com/', 'D:\teste.html')
end;
Facebook Comments Box
  • Giovani Da Cruz
  • 3.874 views
  • 0 comentários
  • 18 de novembro de 2016

Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!

Deixe um comentário

Ir ao topo

© 2024 Infus Soluções em Tecnologia - Todos os Direitos Reservados