Delphi: How to load a text file on the internet into a string?
Question
On my program I have a function that checks for the current version of the program, which it gets from the url: www.tablemaster.webs.com/versioninfo.txt
As you can see, at the URL is just plain text. I need to load this text from the URL into a string on my program, how will I go about doing this? I've searched around but found nothing..
PS: I need the simplest code possible..
Thanks in advance :)
Solution
I would use Indy's TIdHTTP
with it's easiest GET overload this way:
uses
IdHTTP;
procedure TForm1.Button1Click(Sender: TObject);
var
S: string;
IdHTTP: TIdHTTP;
begin
IdHTTP := TIdHTTP.Create(nil);
try
S := IdHTTP.Get('http://www.tablemaster.webs.com/versioninfo.txt');
ShowMessage(S);
finally
IdHTTP.Free;
end;
end;
OTHER TIPS
you can use TIEHTTP component from myfxboard.. to load txt file from url with TIEHTTP:
http.ExecuteURL('www.tablemaster.webs.com/versioninfo.txt');
Memo1.Lines.Add(http.sl.Text);
I would use MSXML2_TLB, especially if I already use XML in the project. MSXML2 is present in Windows since version Internet Explorer 5.5, so chances are real good it's present on the system. You can get MSXML2_TLB.pas using the Import Type Library option from the main menu, and selecting "Microsoft XML, v6.0" (or higher) from the list.
var
r:XMLHTTP;
begin
r:=CoXMLHTTP.Create;
r.open('GET','http://www.tablemaster.webs.com/versioninfo.txt','','');
r.send(EmptyParam);
if r.status<>200 then raise Exception.Create(IntToStr(r.status)+' '+r.statusText);
Result:=r.responseText;
end;