Download remote xml file

I need to know how to download a remote XML file from a server that requires authentication. Using the code below:

procedure TForm1.Button1CLICK(Sender: object); Var xmld : TXMLDocument; begin xmld.LoadFromFile('http://mysite'); xmld.active := true; end; 

I don’t know where to enter user credentials. When I execute, the error is "access denied". Can someone help please. thanks in advance

+6
source share
4 answers

Well, this is actually a two-part question:

  • How to download a document from a server on the Internet that requires authentication?

  • How to load an XML document into an XmlDocument dynamically?

You can use the IdHttp component, which is already available in the Indy package and installed with your Delphi, to get the XML document from the server. To do this, you can call the Get method by passing the address of the XML document as a parameter. You can get the result as a string or stream.

If the server uses authentication, you must first determine which authentication methods it uses; if it uses HTTP authentication, IdHttp already allows you to specify HTTP request parameters by providing the Request property. You can set Username \ Password and other parameters using this property. If it uses cookie authentication, you can associate the cookie manager object with IdHttp and provide the necessary cookie for the server. The server can use the web form for authentication and return a cookie to you or return a session ID. Therefore, it is important to know which authentication method is used by the server.

If you have no idea about the authentication method used by the server, you can ask their support team, or you can install a sniffer, such as Wireshark, and try to connect to the server using a web browser and capture data exchanged between the server and your browser, and analyze it to find out which method is used.

In any case, once you have received the XML data, you can load it into an instance of TXmlDocument using its LoadFromStream method or its XML property.

+6
source

I encoded this as a few days ago. I did auto-update. Here is the code snippet:

 procedure TUpdateForm.GetPage(URL: string); var ms: TMemoryStream; IdHTTP: TIdHTTP; begin SRC.Free; SRC:=TStringList.Create; IdHTTP:=TIdHTTP.Create(); IdHTTP.HandleRedirects:=True; IdHTTP.Request.Username:='USERNAME'; IdHTTP.Request.Password:='PASSWORD'; ms:=TMemoryStream.Create; IdHTTP.Get(URL,ms); ms.Position:=0; TEncoding.UTF8.ToString; Src.LoadFromStream(ms,TENCODING.UTF8); Src.Free; ms.Free; IdHTTP.Free; end; procedure TUpdateForm.GetXML; var TempNode: IXMLNode; TempVersion: TVersion; i,j:integer; begin GetPage('http://www.w3sayit.wz.cz/updateinfo.xml'); MainForm.XMLDocument.LoadFromXML(SRC.Text); MainForm.XMLDocument.Active:=true; ... 

The first procedure loads the document by URL into the global TStringList (SRC). The second parses the XML (I cut the rest of it).

EDIT: sorry, I just read that you want authorization. Will be updated soon. Done.

+3
source

TXMLDocument.LoadFromFile is for simple situations. In your case, you need to download (using http) an xml file with propper credentials, and then use TXMLDocument.LoadFromStream or TXMLDocument.XML to load xml into the parser.

0
source

Not sure if this will work, but you can try:

http: // user: password@yoursite.com / file

This works in my case with HTTP authentication.

0
source

Source: https://habr.com/ru/post/888753/


All Articles