TidHTTP 错误处理

发布于 2024-10-21 19:42:20 字数 1249 浏览 11 评论 0原文

我正在编写一个程序来对网站执行检查以测试它们的当前状态,例如 200 OK。我使用 Delphi 2010 附带的 Indy HTTP 来完成此任务,并拥有当前代码。

procedure TCheckSiteIndividual.Execute;
var
http : TIdhttp;

begin
  try
    try
      http := Tidhttp.create(nil);  //Create indy
      http.Get(CSiteURL,nil);  //Send Request To Check Site
    except
      on E: EIdHTTPProtocolException do
       status := http.ResponseText; // or: code := E.ErrorCode;
    end;

    status := http.ResponseText;   //Return Status of Site
    synchronize(updateform);   //Update data to form
  finally
   http.Free;
  end;
end;

CSiteURL 变量在线程构造函数中设置,status 变量是整个线程的变量。

尝试,除了此代码的部分内容来自 Remy Lebeau - TeamB 此处 这是一个我最初摆弄这个想法时问的问题。我遇到的问题是,此代码仅在网站返回 200 OK 代码时才有效。其他任何情况都会导致异常,包括我禁用互联网。这也会导致重定向网站上的异常,例如 www.google.com 在调试过程中导致异常,我认为它会为我重定向到 www.google.co.uk,但是如果我继续通过异常,那么它会返回 301 FOUND 代码没问题。

我正在寻找它的最终结果是根据网站是否在线或有错误(例如HTML返回代码)给我一个返回,当网站不存在时(例如未注册的URL)另外提供反馈或者就是联系不上。

我正在寻找的其他建议是如何从请求中获取更多信息。具体来说,目前我还想获取所检查网站的 IP。

让它工作而不需要在任何 URL 之前添加 http:// 也很好,因此例如 www.google.com 可以工作,但目前由于协议未知而无法工作。

基本上,如果有人有 IndyHTTP 一些不错的文档的链接,那就太好了,因为它似乎很稀疏,

I'm writing a program to perform checks on websites to test they're current status, e.g. 200 OK. I'm using Indy HTTP shipped with Delphi 2010 for this task and have the current code.

procedure TCheckSiteIndividual.Execute;
var
http : TIdhttp;

begin
  try
    try
      http := Tidhttp.create(nil);  //Create indy
      http.Get(CSiteURL,nil);  //Send Request To Check Site
    except
      on E: EIdHTTPProtocolException do
       status := http.ResponseText; // or: code := E.ErrorCode;
    end;

    status := http.ResponseText;   //Return Status of Site
    synchronize(updateform);   //Update data to form
  finally
   http.Free;
  end;
end;

The CSiteURL variable is set in the Thread Constructor, and the status variable is a variable to the entire thread.

The try, except parts of this code came from Remy Lebeau - TeamB here which was a question I asked when initially fiddling around with this idea. The problem I'm having is that this code only works if the site returns a 200 OK code. Anything else causes an exception, including if I disable the internet. This also causes exceptions on sites that redirect e.g. www.google.com causes an exception during debugging I presume causes it's redirecting to www.google.co.uk for me, however if I continue past the exception then it returns a 301 FOUND code with no problems.

The final result I'm looking for it for it to give me a return based on whether the site is online or having error (e.g. HTML RETURN CODE) additionally giving feedback when the site doesn't exist (e.g. a non-registered URL) or just can't be contacted.

The other peices of advice I'm looking for are into how to get some more information back from the request. Specifically at the moment I'm looking to also get the IP of the site that was checked.

It would also be nice to have it work without needing to have http:// before any URL, so for example www.google.com will work which at the moment it doesn't because of unknown protocol.

Basically it would be great if someone had a link to some nice documentation for IndyHTTP because it seems to be sparse,

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

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

发布评论

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

评论(1

残花月 2024-10-28 19:42:20

是的,当然会导致异常。这就是 Indy 的工作原理,这就是为什么 Remy 建议您捕获异常。

现在您感到困惑的是,即使您有代码来捕获异常,Delphi 调试器仍然会拦截它们并暂停程序的执行以通知您发生了异常。 我之前写过关于这种现象的文章。

因为你显然对了解非-当您调试时出现 status-200 异常 - 因为您的程序设计为能够自行处理所有这些错误 - 您最好的选择可能是将 EIdHTTPProtocolException 添加到调试器的忽略异常列表中。最简单的方法是在调试器下次通知您时选择“忽略此异常类型”。

Yes, of course it causes an exception. That's how Indy works, and that's why Remy advised you to catch exceptions.

What you're confused by now is that even though you have code to catch exceptions, the Delphi debugger still intercepts them and pauses execution of your program to notify you that an exception occurred. I've written about that phenomenon before.

Since you're apparently not interested in knowing about non-status-200 exceptions while you're debugging — because your program is designed to handle all those errors itself — your best option is probably to add EIdHTTPProtocolException to the debugger's list of ignored exceptions. The easiest way to do that is to choose "Ignore this exception type" the next time the debugger notifies you.

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