user863551
user863551

Reputation:

Pulling a Value from a webpage

I'm wanting to pull a single value from a website, the page in question is just a file with no extension that contains a number, no HTML code - nothing.

All I want to do is get my program to pull this figure and compare it to it's own version number. What is going to be the best way about fetching this? I wrote a small snippet to download the file and save it in the temp directory and then processing the figure within the downloaded file but there has to be a better way if I'm not mistaken?

I have read about people using SOAP requests but I'm not sure if this is what I want.

Any help would be appreciated.

Upvotes: 1

Views: 180

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598279

Delphi ships with the Indy components. You can use the Get() method of the TIdHTTP component to download the file contents into a String, eg:

var
  Version: String;
begin
  Version := IdHTTP1.Get('http://url_here');
  ...
end;

Upvotes: 5

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109158

This is how to get a string from a HTTP GET request:

function WebGetData(const UserAgent: string; const Server: string;
  const Resource: string): string; overload;
var
  hInet: HINTERNET;
  hURL: HINTERNET;
  Buffer: array[0..1023] of AnsiChar;
  i, BufferLen: cardinal;
begin
  result := '';
  hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG,
    nil, nil, 0);
  if hInet = nil then RaiseLastOSError;
  try
    hURL := InternetOpenUrl(hInet, PChar('http://' + Server + Resource),
      nil, 0, 0, 0);
    if hURL = nil then RaiseLastOSError;
    try
      repeat
        InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
        if BufferLen = SizeOf(Buffer) then
          result := result + AnsiString(Buffer)
        else if BufferLen > 0 then
          for i := 0 to BufferLen - 1 do
            result := result + Buffer[i];
      until BufferLen = 0;
    finally
      InternetCloseHandle(hURL);
    end;
  finally
    InternetCloseHandle(hInet);
  end;
end;

This is how my own software AlgoSim checks for updates. Try, for instance,

ShowMessage(WebGetData('My User Agent', 'services.rejbrand.se',
  '/algosim/update/ver.asp'));

Upvotes: 5

Related Questions