从 URL 加载图像 - 如果图像不存在,如何捕获异常
我的问题与 我问过的有关从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你如何捕获异常?与捕获任何其他异常的方式相同。使用
try
-except
块。当 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.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 theErrorCode
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 toTIdCustomHTTP.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
ifGet
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 ofEIdHTTPProtocolException
will get re-raised automatically. Do not catch exceptions for errors that you don't know how to resolve.每次尝试都有收获
这是你应该处理异常的地方
可能会创建一个空白图像,上面有一些文本图像不可用
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