0

Return 404ページが見つからない場合、Idhttpをループで実行する方法 は、「GOTO CheckAgain」がTRYステートメントに出入りすることです

label
  CheckAgain;
begin  
  CheckAgain:
  try
    idhttp.Get(sURL+WebFile[I], S);
  except
    on E: EIdHTTPProtocolException do
    if AnsiPos('404',E.Message) <> 0 then
    begin
      I := I+1;
      goto CheckAgain;
    end;
  end;
end;
4

2 に答える 2

3

次の 3 つのオプションがあります。

  1. try/exceptループ内でブロックを使用し、例外をキャッチして、そのプロパティの 404EIdHTTPProtocolException をチェックします。ErrorCode

    repeat
      try
        IdHttp.Get(sURL+WebFile[I], S);
      except
        on E: EIdHTTPProtocolException do
        begin
          if E.ErrorCode <> 404 then
            raise;
          Inc(I);
          Continue;
        end;
      end;
      Break;
    until False;
    
  2. Indy の最新バージョンを使用している場合はhoNoProtocolErrorException、プロパティでフラグを有効にしTIdHTTP.HTTPOptionsてから、ループで を削除して代わりtry/exceptにプロパティを確認できます。TIdHTTP.ResponseCode

    repeat
      IdHttp.Get(sURL+WebFile[I], S);
      if IdHttp.ResponseCode <> 404 then
        Break;
      Inc(I);
    until False;
    
  3. パラメーターTIdHTTP.Get()を持つメソッドのオーバーロードされたバージョンを使用すると、404 応答で例外を発生させないように指示できます。AIgnoreRepliesGet()EIdHTTPProtocolException

    repeat
      IdHttp.Get(sURL+WebFile[I], S, [404]);
      if IdHttp.ResponseCode <> 404 then
        Break;
      Inc(I);
    until False;
    
于 2015-06-12T06:46:56.360 に答える
1

原則として、使用は避けたいと思いgotoます。使用する必要がある場合もありgotoますが、それらはまれであり、問​​題には適していません。

whileループはより一般的に使用されます。最大再試行メカニズムも組み込むことができます。おそらく次のようになります。

RetryCount := 0;
Succeeded := False;
while RetryCount < MaxRetryCount do
  try
    idhttp.Get(sURL+WebFile[I], S);
    Succeeded := True;
    break; // success, break out of while loop
  except
    on E: EIdHTTPProtocolException do
      if E.ErrorCode = 404 then
        inc(RetryCount)
      else
        raise;

  end;
于 2015-06-12T06:35:42.400 に答える