0

I have an VCL app containing an object TDownloadUrl (VCL.ExtActns) used to download applications, my question is how to handle any kind of exception that restrict to download [for example:- like download failed or invalid URL link or internet failure or url not reachable or internet not available] using TDownLoadURL?.

Thanks in advance

2 Answers2

0

TDownloadURL only defines 2 error messages, which are both declared in the Vcl.Consts unit:

  • SUrlMonDllMissing, which is raised when the Win32 URLDownloadToFile() function cannot be accessed at runtime.

  • SErrorDownloadingURL, which is raised when URLDownloadToFile() fails for any reason. Unfortunately, there is no way to differentiate why URLDownloadToFile() fails (although the OnProgress event may provide information about what it was doing just before the failure occurred).

The error messages are resource strings and thus can be localized, so they could potentially be in any language, not just English. And they are raised using the general SysUtils.Exception class itself, not any derived types. However, you can use them for substring matching, at least:

uses
  ..., Vcl.ExtActns, Vcl.Consts, System.StrUtils;

try
  DownloadURL1.Filename := ...;
  DownloadURL1.URL := ...;
  DownloadURL1.Execute;
except
  on E: Exception do
  begin
    if StartsText(SUrlMonDllMissing, E.Message) then
      ...
    else if StartsText(SErrorDownloadingURL, E.Message) then
      ...
    else
      ...
  end;
end;

If you need more detailed error information, you might try calling URLDownloadToFile() directly, as it returns an HRESULT value. However, be careful by the following gotcha in the documentation:

URLDownloadToFile returns S_OK even if the file cannot be created and the download is canceled. If the szFileName parameter contains a file path, ensure that the destination directory exists before calling URLDownloadToFile. For best control over the download and its progress, an IBindStatusCallback interface is recommended.

If that does not solve your issue, then you should use a different HTTP client API/library to perform the download, such as the HTTP client in Indy, ICS, Synapse, WinInet/WinHTTP, libCURL, etc.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
-1

I've not used this component, but it likely generates different exception types based on the errors it encounters. If that's the case then the article here covers handling multiple exception types:

Delphi Exception handling problem with multiple Exception handling blocks

Community
  • 1
  • 1
Owen
  • 90
  • 1
  • 2
  • 5
  • link you have shared says multiple Exception handling blocks. But my question was how to classified the particular exception message, for example if we are trying to download from a invalid URL then exception should show as it is "invalid URL" or "URL not exit". I'm new to TDownLoadURL and in exception block didn't find any classified message is there if download is failed. – Santosh Nayak Oct 19 '16 at 13:52
  • I just created a quick sample project to see how the component works. Its exceptions are all simply of the Exception type, so my earlier suggestion is useless, sorry. There was error text included with each exception though. When trying to download a page I knew didn't exist, for example, I got an exception whose message property was ("Error Downloading URL: "). if those messages are consistent (I have no idea if they are), you might be able to use them to determine the error. Have you considered using the Indy component TIdHTTP instead? – Owen Oct 19 '16 at 14:24