0

看看yaw能不能帮到我

假设有一个链接:www.example.com/test.html

打开时,它将显示 0 或 1。

我需要获取该值。IE:

if internet.value := 0 then ShowMessage('False') else ShowMessage('True');

它可能使用 indy 组件或 winsockets,我将如何处理这个?

4

1 回答 1

3

如果你在谈论一个只包含一个整数值的纯文本文件,你可以使用 Indy,例如这种方式。以下函数在页面下载成功且页面包含整数值时返回 True,否则返回 False。请注意,我是在浏览器中编写的,因此未经测试:

uses
  IdHTTP;

function TryWebContentToInt(const AURL: string; out AValue: Integer): Boolean;
var
  S: string;
  IdHTTP: TIdHTTP;
begin
  IdHTTP := TIdHTTP.Create(nil);
  try
    IdHTTP.HandleRedirects := True;
    try
      S := IdHTTP.Get(AURL);
      Result := TryStrToInt(S, AValue);
    except
      Result := False;
    end;       
  finally
    IdHTTP.Free;
  end;
end;

以及用法:

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
begin
  if TryWebContentToInt('http://example.com/page.html', I) then
    ShowMessage('Value: ' + IntToStr(I))
  else
    ShowMessage('Page downloading failed or it doesn''t contain an integer value!');
end;
于 2012-10-08T07:27:55.607 回答