| If you are considering Delphi XE as cross compiler for the multiple platforms such as Android, iOS, and Windows, you need to have the standard method to get web content such as the normal text, or XML document as a result of RESTful API. Delphi provides the straightforward, and easy way can implement it. The followings are two reference example can implement it. # Using Indy's TIdHTTP 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;
The above is the easiest way can download the webpage. And the below is the another example can download that based on TIdHTTP. uses IdHTTP;
function ReadFromWeb(url:string):string;
const
cUSER_AGENT = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543 Safari/419.3';
var
IdHTTP: TIdHTTP;
Stream: TStringStream;
begin
Result := '';
IdHTTP := TIdHTTP.Create(nil);
Stream := TStringStream.Create;
try
IdHTTP.Request.UserAgent := cUSER_AGENT;
try
IdHTTP.Get(url, Stream);
Result := Stream.DataString;
except
Result := '';
end;
finally
Stream.Free;
IdHTTP.Free;
end;
end;
Feeling is like that the second example is better stable than the first one based on my test. # How to read XML This is one of the easiest way can get XML document as a result of RESTful API. procedure TForm1.Button1Click(Sender: TObject);
var
XMLDoc: IXMLDOMDocument;
begin
XMLDoc := CoDOMDocument.Create;
XMLDoc.load('MyBooks.xml');
Memo1.Lines.Text := XMLDoc.xml;
end; |