-1

I have a txt uploaded in my site where the content of this txt file is my Noip address that must be consulted by a http request.

The trouble in my code below is that if this file was removed of site, is generated a exception and not is possible execute the folowing lines for function returns a right result.

How solve it?

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  idhttp,
  SysUtils;

function GetURLAsString(const aURL, bURL: string): string;
var
  lHTTP: TIdHTTP;
  a, b: string;
begin
  lHTTP := TIdHTTP.Create(nil);
  try
    a := lHTTP.Get(aURL);
     if lhttp.responsecode = 200 then
      begin
        Result := a
      end
      else
      begin
       b := lHTTP.Get(bURL);
       if lHTTP.ResponseCode = 200 then
        Result := b
        else
         Result := 'mydnsname.ddns.net';
      end;
  finally
    lHTTP.Free;
  end;
end;

begin
  try
    GetURLAsString('http://www.mysite01.com/conn.txt','http://www.mysite02.com/conn.txt');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

thanks in advance.

EDIT

the result of GetURLAsString must be 'mydnsname.ddns.net' if some of two request fail. Else, the result must be content of txt file uploaded in my site ( that is the same mydnsname.ddns.net ).

  • Are you saying the function GetURLAsString does not generate an exception if the file is missing? If so just raise an exception yourself instead of assigning 'mydnsname.ddns.net' to Result. Or don't use exceptions and test the result for this string. Or change the string to an empty string and test for that. – Dsm Jul 07 '17 at 14:39
  • Then I am sorry, but what is your question? – Dsm Jul 07 '17 at 14:44
  • Are you saying lHTTP.Get(aURL) generates an exception in this case? – Dsm Jul 07 '17 at 14:46
  • @Dsm, yes, but only if txt file not is found in my site. –  Jul 07 '17 at 14:48
  • @Dsm, question edited. –  Jul 07 '17 at 14:50

1 Answers1

4

By design, TIdHTTP raises an EIdHTTPProtocolException exception by default if the HTTP server returns a failure response code, like 404 NOT FOUND.

There are several different ways you can handle this:

  1. Catch the exception:

    function GetURLAsString(const aURL, bURL: string): string;
    var
      lHTTP: TIdHTTP;
    begin
      lHTTP := TIdHTTP.Create(nil);
      try
        try
          Result := lHTTP.Get(aURL);
        except
          try
            Result := lHTTP.Get(bURL);
          except
            Result := 'mydnsname.ddns.net';
          end;
        end;
      finally
        lHTTP.Free;
      end;
    end;
    
  2. Pass 404 in the AIgnoreReplies parameter of Get() so the exception is not raised on 404 errors, but still raised on other errors:

    function GetURLAsString(const aURL, bURL: string): string;
    var
      lHTTP: TIdHTTP;
    begin
      lHTTP := TIdHTTP.Create(nil);
      try            
        Result := lHTTP.Get(aURL, [404]);
        if lHTTP.ResponseCode = 200 then Exit;
        Result := lHTTP.Get(bURL, [404]);
        if lHTTP.ResponseCode = 200 then Exit;
        Result := 'mydnsname.ddns.net';
      finally
        lHTTP.Free;
      end;
    end;
    
  3. Enable the hoNoProtocolErrorException flag in the HTTPOptions property so the exception is not raised at all:

    function GetURLAsString(const aURL, bURL: string): string;
    var
      lHTTP: TIdHTTP;
    begin
      lHTTP := TIdHTTP.Create(nil);
      try
        lHTTP.HTTPOptions := lHTTP.HTTPOptions + [HoNoProtocolErrorException];      
        Result := lHTTP.Get(aURL);
        if lHTTP.ResponseCode = 200 then Exit;
        Result := lHTTP.Get(bURL);
        if lHTTP.ResponseCode = 200 then Exit;
        Result := 'mydnsname.ddns.net';
      finally
        lHTTP.Free;
      end;
    end;
    
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770