-1

Hoping for help with TDownloadURL Using this code that saves downloaded file. Using Delphi XE2. want to save download to memory/tstringlist not file. how to do this without adding component? Thank you!

notfound:=false;
dl := TDownloadURL.Create(self);
  try
    dl.URL := url;
    dl.FileName := execpath+'apic1.csv'; dl.ExecuteTarget(nil); dl.Free;
  except
    dl.Free;
    notfound:=true;
  end;
kualoa
  • 17
  • 1
  • 4
  • This code is pretty quaint, the way it handles exceptions and lifetime. If you don't want to use this component, perhaps you can decide what code you are permitted to use. Once you know that, why not write it yourself? – David Heffernan Aug 10 '17 at 14:57
  • 1
    Then don't use TDownloadURL. There are many suitable ways to retrieve content from a URL without it. `TIdHTTP`, for instance, has been part of Delphi for longer than a decade. – Ken White Aug 10 '17 at 15:50

1 Answers1

7

The TDownLoadURL from ExtActns unit can't do anything more than downloading to a file. It's ExecuteTarget method is implemented like that. If I were you, I would use Indy. It's simple:

uses
  IdHTTP;

var
  Client: TIdHTTP;
  Stream: TStream;
begin
  Client := TIdHTTP.Create;
  try
    Stream := TMemoryStream.Create;
    try
      Client.Get(URL, Stream);
      { ← process Stream somehow }
    finally
      Stream.Free;
    end;
  finally
    Client.Free;
  end;
end;
Victoria
  • 7,822
  • 2
  • 21
  • 44
  • Thank you. Running this code verbatim, I get the error "IOHandler is not valid" when processing this statement: Client.Get(URL, Stream); ... URL is a valid URL ... thoughts on a solution? – kualoa Aug 12 '17 at 10:26
  • Do you target HTTPS? – Victoria Aug 12 '17 at 10:38
  • Thank you ... these both raise the error: url:='https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address=4600+Silver+Hill+Rd%2C+Suitland%2C+MD+20746&benchmark=9&format=json'; url:= 'https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=0&companyName=&address1=1600+PENNSYLVANIA+AVE+NW&address2=&city=&state=Select&urbanCode=&postalCode=&zip=20500'; – kualoa Aug 12 '17 at 16:56
  • Then follow [this post](https://stackoverflow.com/a/11562655/8041231), just don't forget the last step mentioned there (get OpenSSL library). – Victoria Aug 12 '17 at 17:05
  • Thank you. using that code, following snippet returns good "SRC" value when using the USPS url but raises exception with the census url. Cannot see why. try IdHTTP1.IOHandler:=LHandler; Src:= IdHTTP1.Get( 'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address=4600+Silver+Hill+Rd%2C+Suitland%2C+MD+20746&benchmark=9&format=json'); // 'https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=0&companyName=&address1=1600+PENNSYLVANIA+AVE+NW&address2=&city=&state=Select&urbanCode=&postalCode=&zip=20500'); – kualoa Aug 13 '17 at 20:09
  • That's a different thing. These are so called parameters. You need to [encode them manually](https://stackoverflow.com/a/17861261/8041231), or pass as a parameter. – Victoria Aug 14 '17 at 08:42
  • Thank you. that failed also -- Src:= IdHTTP1.Get(TIdURI.URLEncode('https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address=4600+Silver+Hill+Rd%2C+Suitland%2C+MD+20746&benchmark=9&format=json')); -- what have I got wrong? – kualoa Aug 14 '17 at 17:03