I need to know how to load remote xml file from a server that needs authentication. Using the code bellow :
procedure TForm1.Button1CLICK(Sender: object);
Var xmld : TXMLDocument;
begin
xmld.LoadFromFile('http://mysite');
xmld.active := true;
end;
I dont know where to put user credentials. When I execute, error "access denied" occures. Can anybody help please. Thanks in advance
Well this is actually a two-part question:
How to download a document from a server on internet which requires authentication?
How to load an XML document into XmlDocument object dynamically?
You can use IdHttp component, which is already available in Indy package and installed with your Delphi, to retrieve the XML document from the server. To do this, you can call its Get method, passing XML document address as a parameter. You can retrieve the result as a string or a stream.
If the server is using authentication, then you should first detect what kind of authentication methods it is using; if it is using HTTP authentication, IdHttp already allows you to define HTTP request parameters by providing a Request property. You can set Username\Password and other parameters using this property. If it using a cookie-based authentication, you can connect a cookie manager object to IdHttp and provide the required cookie to the server. The server might use a web form for authentication and return the cookie back to you, or return a session id. So it is important you know what authentication method the server is using.
If you have no idea about the authentication method used by server, you can ask their support team, or you can install a sniffer like Wireshark, and try to connect to the server using you web browser, and capture the data exchanged between the server and your browser, and analyze it to find out what method is used.
Anyways, once you have received the XML data, you can load it into a TXmlDocument instance using its LoadFromStream method, or its XML property.
I coded this like a few days ago. I was implementing an auto-updater. Here is a chunk of the code:
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 an document at URL to a global TStringList (SRC). The second one parses the XML (i cut the rest of it).
EDIT: sorry, I just read you want an auth. Will update soon. Done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With