加载远程xml文件 [英] Load remote xml file

查看:69
本文介绍了加载远程xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道如何从需要身份验证的服务器上加载远程xml文件.使用下面的代码:

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:

  1. 如何从互联网上的服务器,需要身份验证?

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

如何将XML文档加载到XmlDocument对象是动态的吗?

How to load an XML document into XmlDocument object dynamically?

您可以使用Indy包中已提供并与Delphi一起安装的IdHttp组件从服务器检索XML文档.为此,您可以调用其Get方法,并将XML文档地址作为参数传递.您可以将结果检索为字符串或流.

You can use IdHttp component, which is already available in Indy package and installed with your Delp 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.

如果服务器正在使用身份验证,那么您首先应该检测服务器正在使用哪种身份验证方法;否则,请执行以下操作.如果正在使用HTTP身份验证,则IdHttp已经允许您通过提供Request属性来定义HTTP请求参数.您可以使用此属性设置Username \ Password和其他参数.如果它使用基于cookie的身份验证,则可以将cookie管理器对象连接到IdHttp并将所需的cookie提供给服务器.服务器可能使用Web表单进行身份验证,然后将Cookie返回给您,或者返回会话ID.因此,重要的是要知道服务器正在使用哪种身份验证方法.

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.

如果您对服务器使用的身份验证方法一无所知,可以咨询他们的支持团队,或者安装诸如Wireshark的嗅探器,然后尝试使用Web浏览器连接到服务器,并捕获在服务器之间交换的数据.服务器和浏览器,并对其进行分析以找出使用哪种方法.

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.

无论如何,一旦收到XML数据,就可以使用其LoadFromStream方法或其XML属性将其加载到TXmlDocument实例中.

Anyways, once you have received the XML data, you can load it into a TXmlDocument instance using its LoadFromStream method, or its XML property.

这篇关于加载远程xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆