从 URL 加载图像 - 如果图像不存在,如何捕获异常

发布于 2024-08-05 17:52:20 字数 793 浏览 3 评论 0原文

我的问题与 我问过的有关从 URL 加载图像的其他问题有关。 下面的代码有效。 但是,如果我尝试获取图像(它应该在那里),我就遇到了问题,但是 显然已被删除。应用程序不知道并返回错误。 有没有办法通过捕获传入错误来防止它。 我将不胜感激任何建议。 这发生在他的线路上:

 IdHTTP1.get('http://www.google.com/intl/en_ALL/images/logo.gif',MS);

谢谢, 克里斯

uses
  GIFImg;

 procedure TForm1.btn1Click(Sender: TObject);
 var
   MS : TMemoryStream;
   GIf: TGIFImage;
 begin
     MS := TMemoryStream.Create;
    GIf := TGIFImage.Create;
   try
    IdHTTP1.get('http://www.google.com/intl/en_ALL/images/logo.gif',MS);
    Ms.Seek(0,soFromBeginning);       
   Gif.LoadFromStream(MS);
   img1.Picture.Assign(GIF);

  finally
   FreeAndNil(GIF);
   FreeAndNil(MS);
  end;
end;

My problem relates to the other question I have asked about loading image from the URL.
The code below works.
However, I have encountered the problem if I try to get the image (it suppose be there) but
apparently was removed. The application does not know and returns the error.
Is there a way to prevent it by capturing incoming error.
I would appreciate any suggestions.
It happens at his line:

 IdHTTP1.get('http://www.google.com/intl/en_ALL/images/logo.gif',MS);

Thanks,
Chris

uses
  GIFImg;

 procedure TForm1.btn1Click(Sender: TObject);
 var
   MS : TMemoryStream;
   GIf: TGIFImage;
 begin
     MS := TMemoryStream.Create;
    GIf := TGIFImage.Create;
   try
    IdHTTP1.get('http://www.google.com/intl/en_ALL/images/logo.gif',MS);
    Ms.Seek(0,soFromBeginning);       
   Gif.LoadFromStream(MS);
   img1.Picture.Assign(GIF);

  finally
   FreeAndNil(GIF);
   FreeAndNil(MS);
  end;
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦一生花开无言 2024-08-12 17:52:21

你如何捕获异常?与捕获任何其他异常的方式相同。使用 try-except 块。

procedure TForm1.btn1Click(Sender: TObject);
var
  MS : TMemoryStream;
  Gif: TGIFImage;
begin
  MS := TMemoryStream.Create;
  try
    Gif := TGIFImage.Create;
    try
      try
        IdHTTP1.Get('http://www.google.com/intl/en_ALL/images/logo.gif', MS);
      except
        on e: EIdHTTPProtocolException do begin
          if e.ErrorCode = 404 then begin
            // not found
            exit;
          end else begin
            // some other reason for failure
            raise;
          end;
        end;
      end;
      MS.Position := 0;
      Gif.LoadFromStream(MS);
      img1.Picture.Assign(Gif);
    finally
      Gif.Free;
    end;
  finally
    MS.Free;
  end;
end;

当 Indy 收到不可接受的响应时,它会引发 EIdHTTPProtocolException 类型的异常。它不会针对每种可能的失败原因引发不同的异常类,因此请检查 ErrorCode 属性以获取数字响应。有关各种响应代码的含义,请参阅 HTTP 规范

看起来 Indy 可以通过 TIdCustomHTTP.DoRequest 方法的 AIgnoreReplies 参数配置为忽略某些响应代码,但我已经追踪到该参数了愿意今天去,所以如果您想了解更多相关信息,请提出一个新问题,或者有人可以编辑此答案以用更完整的内容替换此段落。

在上面的示例中,我检查了 404(未找到)并退出。如果 Get 引发异常,请确保不要使用 MS,因为它将包含不确定的内容,并且可能不是有效的 GIF 图像。对于任何其他服务器响应,我重新引发异常,以便调用者可以处理它。您可能想要改变这一点。任何不是 EIdHTTPProtocolException 后代的异常都将自动重新引发。不要捕获您不知道如何解决的错误的异常。

How do you capture the exception? The same way you capture any other exception. Use a try-except block.

procedure TForm1.btn1Click(Sender: TObject);
var
  MS : TMemoryStream;
  Gif: TGIFImage;
begin
  MS := TMemoryStream.Create;
  try
    Gif := TGIFImage.Create;
    try
      try
        IdHTTP1.Get('http://www.google.com/intl/en_ALL/images/logo.gif', MS);
      except
        on e: EIdHTTPProtocolException do begin
          if e.ErrorCode = 404 then begin
            // not found
            exit;
          end else begin
            // some other reason for failure
            raise;
          end;
        end;
      end;
      MS.Position := 0;
      Gif.LoadFromStream(MS);
      img1.Picture.Assign(Gif);
    finally
      Gif.Free;
    end;
  finally
    MS.Free;
  end;
end;

When Indy gets an unacceptable response, it raises an exception of type EIdHTTPProtocolException. It does not raise a different exception class for each possible failure reason, so check the ErrorCode property for the numeric response. Refer to the HTTP spec for the meanings of the various response codes.

It looks like Indy can be configured to ignore certain response codes via the AIgnoreReplies argument to TIdCustomHTTP.DoRequest method, but I've traced that parameter back as far as I'm willing to go today, so if you want more information about that, either ask a new question or someone can edit this answer to replace this paragraph with something more complete.

In my example above, I have checked for 404 (not found) and exit. Make sure you don't use MS if Get raises an exception because it will have indeterminate contents, and probably not a valid GIF image. For any other server response, I re-raise the exception so the caller can handle it instead. You'll probably want to change that. Any exception that's not a descendant of EIdHTTPProtocolException will get re-raised automatically. Do not catch exceptions for errors that you don't know how to resolve.

め七分饶幸 2024-08-12 17:52:21

每次尝试都有收获
这是你应该处理异常的地方
可能会创建一个空白图像,上面有一些文本图像不可用

every try has a catch
it is there the place where you should treat your exceptions
probably creating a blank image with some text on it Image Not Available

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文