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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
PROGRAM test2;
{$APPTYPE CONSOLE}
uses
Forms,
Sysutils,
IdComponent,
IdHttp,
IdSSLOpenSSL,
CLASSES;
VAR IdHttp1:TidHttp;
IdSSL1:TIdSSLIOHandlerSocketOpenSSL;//assume on Form
Lieux: TstringList;
SizeOfFile:Longint;
Zdown1:String;
type
TForm1 = class(TForm)
procedure IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
procedure IdHTTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
procedure IdHTTPWorkEnd(Sender: TObject; AWorkMode: TWorkMode);{}
// procedure FormCreate(Sender: TObject);
private
{ private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure TForm1.IdHTTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
if (AWorkMode=wmRead) and (SizeOfFile<>0) then WRITE(ExtractFilename(Zdown1)+':'+inttostr(trunc(AWorkCount/SizeOfFile*100))+'% '+#13);
end;
procedure TForm1.IdHTTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
if AWorkMode = wmRead then // Uniquement quand le composant reçoit des données
begin
SizeOfFile := AWorkCountMax; // Maximum = taille de l'élément
WRITE(ExtractFilename(Zdown1)+':0% '+#13);
end;
end;
procedure TForm1.IdHTTPWorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
WRITELN(ExtractFilename(Zdown1)+':100% '+#13);
end;
FUNCTION Downloadfilehttps(var selfile:TSTRINGLIST;url,destination:STRING;html:boolean): BOOLEAN;
VAR stream:TMemoryStream;
BEGIN
result:=False;
stream:=TMemoryStream.Create;
if html then
begin
idHttp1.OnWorkBegin := nil;
idHttp1.OnWork := nil;
idHttp1.OnWorkEnd := nil;
end
ELSE
begin
idHttp1.OnWorkBegin := Form1.IdHTTP1WorkBegin;
idHttp1.OnWork := Form1.IdHTTP1Work;
idHttp1.OnWorkEnd := Form1.IdHTTPWorkEnd;
end;
try
idHttp1.Get(url, stream);
result:=IdHTTP1.ResponseCode=200;
except
on E: EIdHTTPProtocolException DO BEGIN END;
on E: Exception DO BEGIN END;
end;
stream.Position := 0;
if html then
begin
selfile.Clear;
selfile.LoadFromStream(stream);
end
else
begin
stream.SaveToFile(destination);
end;
stream.Free;
END;
BEGIN
lieux:=TSTRINGLIST.Create;
idHttp1 := TIdHTTP.Create(NIL);
IdSSL1:=TIdSSLIOHandlerSocketOpenSSL.Create(NIL);
WITH IdSSL1 DO
BEGIN
SSLOptions.Method := sslvTLSv1_2;
SSLOptions.SSLVersions := [sslvTLSv1_2];
SSLOptions.Mode := sslmClient;
SSLOptions.VerifyMode := [];
SSLOptions.VerifyDepth := 0;
PassThrough := false;
END;
WITH idHttp1 DO
BEGIN
IOHandler := IdSSL1;
AllowCookies := True;
HandleRedirects := True;
OnWorkBegin := Form1.IdHTTP1WorkBegin;
OnWork := Form1.IdHTTP1Work;
OnWorkEnd := Form1.IdHTTPWorkEnd;
Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0';
Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
Request.AcceptLanguage := 'en-GB,en;q=0.5';
Request.Connection := 'keep-alive';
Request.ContentType := 'application/x-www-form-urlencoded';
END;
Zdown1:='PrixCarburants_instantane.zip';
Downloadfilehttps(Lieux,'https://donnees.roulez-eco.fr/opendata/instantane',Zdown1,false); // lecture du fichier 'https://donnees.roulez-eco.fr/opendata/instantane' en zip
if Downloadfilehttps(Lieux,'https://prix-carburants-info.fr/station-service-total/meximieux/01800001.html','',true) then writeln(pred(lieux.count)) else writeln('erreur 404'); // passe (contenu dans le fichier zip)
if Downloadfilehttps(Lieux,'https://prix-carburants-info.fr/station-service-total/miribel/01700005.html','',true) then writeln(pred(lieux.count)) else writeln('erreur 404'); // erreur 404 dans l'editeur
lieux.Free;
writeln('Appuyez sur une touche');
readln; {}
END. |
Partager