Forum > Networking and Web Programming

[SOLVED] How to download HTML from secure https site

(1/3) > >>

bobonwhidbey:
I've been using the following:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function DownloadHTTP(URL, TargetFile: string): boolean;var  HTTPSender: THTTPSend;begin  Result := False;  HTTPSender := THTTPSend.Create;  try    HTTPSender.HTTPMethod('GET', URL);    if (HTTPSender.ResultCode >= 100) and (HTTPSender.ResultCode <= 299) then    begin      HTTPSender.Document.SaveToFile(TargetFile);      Result := True;    end;  finally    HTTPSender.Free;  end;end;  
to download the HTML from HTTP web site. Is there a similar function I can use to download from HTTPS sites?

CCRDude:
Yes, the same, but you need to make sure you add a SSL library to the uses list... like ssl_openssl (also from synapse) :)

Thaddy:
Yes, I recognize that code  :D O:-)
It is indeed sufficient to add ssl_openssl to the uses clause. Nothing more required.
Full program:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$ifdef fpc}{$mode delphi}{$H+}{$endif}uses sysutils,classes,httpsend, ssl_openssl; function DownloadHTTP(URL, TargetFile: string): boolean;var  HTTPSender: THTTPSend;begin  Result := False;  HTTPSender := THTTPSend.Create;  try    HTTPSender.HTTPMethod('GET', URL);    if (HTTPSender.ResultCode >= 100) and (HTTPSender.ResultCode <= 299) then    begin      HTTPSender.Document.SaveToFile(TargetFile);      Result := True;    end;  finally    HTTPSender.Free;  end;end;  var  s:string ='testme.html';  l:Tstringlist;begin  if fileexists(s) then deletefile(s);   if Downloadhttp('https://www.freepascal.org/index.html',s) then  try    l:=Tstringlist.create;    l.LoadfromFile(s);    writeln(l.text);  finally    l.free;  end;end.
Or much simpler, like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$ifdef fpc}{$mode delphi}{$H+}{$endif}uses classes, httpsend, ssl_openssl;var  l:Tstrings;begin  l:=Tstringlist.create;  try    // this is a standard utility function from synapse    if HttpGetText('https://www.freepascal.org/index.html',L) then writeln(l.text); // or l.savetofile  finally    l.free;  end;end.
That's really all...



bobonwhidbey:
Thank you - works perfectly.

bobonwhidbey:
While MyApp seems to work perfectly, I am getting a compiler warning that I would like to know how to eliminate.

"Warning: other unit files search path (aka unit paty) of "MyApp" contains "C:\Lazarus\components\custom\Synapse\source\lib", which belongs to package "laz_synapse".

I have added the laz_synapse package to the Project Inspector.

Navigation

[0] Message Index

[#] Next page

Go to full version