开发者

Load remote xml file

开发者 https://www.devze.com 2023-03-07 19:24 出处:网络
I need to know how to load remote xml file from a server that needs authentication. Using the code bellow :

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 : TXMLDocu开发者_Go百科ment;
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:

  1. How to download a document from a server on internet which requires authentication?

  2. 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.


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


Not sure if that will work but you can try:

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

This works in my case with HTTP auth.

0

精彩评论

暂无评论...
验证码 换一张
取 消