Gem
Gem

Reputation: 526

get generated xml from internet into delphi app

im working in Delphi XE6 and need to download xml data that is generated by php script on server. I did try with Indy IdHTTP.Get but it doesn't download anything... When i try open same link in browser or in TWebBrowser it shows generated xml data, but when i try to directly pull it with IdHTTP Get, nothing is downloaded, like there is no generated data...

Memo1.Lines.Add := idHTTP.Get(url);

Gives empty memo1.

Any tips or examples how to first execute php script on some server that will generate data, and pull down that result?

Upvotes: 0

Views: 2923

Answers (3)

Gem
Gem

Reputation: 526

Well after trying and searching, i did change

UserAgent instead of Mozilla/3.0 into Mozilla/5.0

and it works now. And yes, xmlStream.Position := 0;

Thnx for help!

Upvotes: 0

Passella
Passella

Reputation: 709

you can do this:

procedure TForm1.Button1Click(Sender: TObject);
var
   dow: TIdHTTP;
   xmlDoc: TXMLDocument;
   xmlStrem: TMemoryStream;
begin
   dow := nil;
   xmlDoc := nil;
   xmlStrem := nil;
   try
      try
         dow := TIdHTTP.Create(Self);
         dow.HandleRedirects := True;
         xmlStrem := TMemoryStream.Create();
         dow.Get('http://url.com/path/', xmlStrem);
         xmlDoc := TXMLDocument.Create(Self);
         xmlDoc.LoadFromStream(xmlStrem);
         ShowMessage(xmlDoc.XML.Text);
      except
         on E: Exception do
         begin
            raise;
         end;
      end;
   finally
      if Assigned(dow) then FreeAndNil(dow);
      if Assigned(xmlDoc) then FreeAndNil(xmlDoc);
      if Assigned(xmlStrem) then FreeAndNil(xmlStrem);
   end;
end;

Upvotes: 5

Remy Lebeau
Remy Lebeau

Reputation: 597941

You are calling the version of TIdHTTP.Get() that returns a UnicodeString, so TIdHTTP will receive the raw XML data and decode it to UTF-16 using the XML's specified charset. If there is a problem determining that charset and/or decoding the data, a blank string may be returned without an exception being raised.

XML is a binary format, subject to its charset. So you really should be downloading the XML as binary, such as with a TMemoryStream, and then passing that as-is to your XML parser for processing. Let it deal with the XML's charset, don't let Indy handle it.

Upvotes: 1

Related Questions