user1727909
user1727909

Reputation: 3

Delphi 7 - Fetch value from a website

let's see if yaw can help me out here,

Supposing there's a link: www.example.com/test.html

Upon opening, it would show either 0 or 1.

I need to fetch that value. I.e.:

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

It could be using indy components or winsockets, how would I go about this one?

Upvotes: 0

Views: 956

Answers (1)

TLama
TLama

Reputation: 76733

If you're talking about a plain text file containing just an integer value, you can use Indy for this e.g. this way. The following function returns True, when the page downloading succeeded and when the page contains an integer value, False otherwise. Please note, that I wrote it in browser so it's untested:

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;

And the usage:

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;

Upvotes: 3

Related Questions