我需要通过.Net网络服务获取存储在sql server数据库中的1000个XML文件,以便从iPhone拨打电话?请建议在.net网络服务中处理它的更好方法吗?
答案 0 :(得分:1)
听起来你在写一个iPhone应用程序?
您不需要SOAP Web服务。您尚未指定客户端在请求时是否知道数千个xml文件的列表。此答案假定客户端明确知道列表。
<强>客户端强>
查找允许iPhone应用程序从HTTP资源读取XML的现有模块或代码。您将点击http://foo.com/bar/GetFile.ashx?file=1.xml
之类的网址
能够以XML格式读取响应的内容。对要下载到手机的每个xml文件重复此调用。
服务器强>
设置Web应用程序以处理文件请求。 创建一个新的.ashx Web处理程序类来侦听和处理xml文件的请求。 这是一个简单示例来说明服务器端:
public class GetFile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse r = context.Response;
r.ContentType = "text/xml";
string requestedFile = context.Request.QueryString["file"];
//ensure you check that the value received on the querystring is valid.
string xmlFromDatabase = MyDataLayer.GetXmlByFileName(requestedFile);
//pump the XML back to the caller
r.Write(xmlFromDatabase);
}
public bool IsReusable{get{return false;}}
}
所有文件在运行时不可用
如果您的客户端不知道文件的名称,只需在该处理程序上创建一个新方法。它可以发布它能够提供服务的可用xml文件列表。
http://foo.com/bar/ListFiles.ashx
public class ListFiles : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse r = context.Response;
r.ContentType = "text/plain";
//grab a listing of all the files from the database
var listFiles = MyDataLayer.GetAllXmlFilesAvailable();
foreach (var file in listFile)
{
r.Write(file + "|");
}
}
public bool IsReusable{get{return false;}}
}