DAV研究(一)

Posted on 2004-05-28 08:56  kevin  阅读(1561)  评论(0编辑  收藏  举报

DAV( http://www.webdav.org/ )
DAV不仅被看作HTTP的扩展,甚至被看作一种网络文件系统(network filesystem );
A final goal of DAV is to leverage the success of HTTP in being a standard access layer for a wide range of storage repositories -- HTTP gave them read access, while DAV gives them write access.
当然,它不可能想本地文件系统一样随心所欲,它支持的操作还是有限的,以下三条:
1。支持锁定,Client可以先Lock Server上的file,然后操作;
2。查找、定位,即DASL协议,DAV的一个子协议;
3。拷贝、移动、创建、列举,:不知道为什么叫做Namespace manipulation;
用FrontPage、Excess等打开、编辑远程网站用的就是这个协议,它使得FrontPage可以编辑、覆盖、删除Server上的文件。

首先看看DASL协议是如何查找Server端的文件系统:
最新草案:http://greenbytes.de/tech/webdav/draft-reschke-webdav-search-latest.html
总原则:使用Request发送XML格式的查询字符串(Request-URI),得到Response,包含查询结果(也是XML格式);
Request-URL语法的一般形式:
<d:searchrequest xmlns:d="DAV:">
  <d:basicsearch>
    <d:select>
      <d:prop><d:getcontentlength/></d:prop>
    </d:select>
    <d:from>
      <d:scope>
        <d:href>/container1/</d:href>
        <d:depth>infinity</d:depth>
      </d:scope>
    </d:from>
    <d:where>
      <d:gt>
        <d:prop><d:getcontentlength/></d:prop>
        <d:literal>10000</d:literal>
      </d:gt>
    </d:where>
      <d:orderby>
        <d:order>
        <d:prop><d:getcontentlength/></d:prop>
        <d:ascending/>
      </d:order>
    </d:orderby>
  </d:basicsearch>
</d:searchrequest>

对于DAV:SELECT 和 DAV:PROP的解释在 http://www.ietf.org/rfc/rfc2518.txt
下面是获取url下所有目录、文件信息的C#代码,目的就是组合一个 HTTP头+Request-URL
                        // url指定Server端的检索目录,我认为也可以通过Request-URL的DAV:where部分来定:
                        HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(url);
                        Request.Headers.Add("Translate: f");
                        Request.Credentials = CredentialCache.DefaultCredentials;

                        string requestString = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
                                "<a:propfind xmlns:a=\"DAV:\">"+
                                "<a:prop>"+
                                "<a:displayname/>"+
                                "<a:iscollection/>"+
                                "<a:getlastmodified/>"+
                                "</a:prop>"+
                                "</a:propfind>";

                        MessageBox.Show(requestString.ToString());// 只是显示一下Request-URL

                        Request.Method = "PROPFIND"; // 有 GET 、POST、PROPFIND.....
                        if (deep == true)                            // 设定服务器上的检索深度
                                Request.Headers.Add("Depth: infinity");
                        else
                                Request.Headers.Add("Depth: 1");
                        Request.ContentLength = requestString.Length;
                        Request.ContentType        = "text/xml";

                        Stream requestStream = Request.GetRequestStream();
                        requestStream.Write(Encoding.ASCII.GetBytes(requestString),0,Encoding.ASCII.GetBytes(requestString).Length);
                        requestStream.Close();

                        HttpWebResponse Response;
                        StreamReader respStream;
                        try
                        {
                                Response = (HttpWebResponse)Request.GetResponse();
                                respStream = new StreamReader(Response.GetResponseStream());
                        }
                        catch (WebException e)
                        {
                               .....
                        }

Copyright © 2024 kevin
Powered by .NET 9.0 on Kubernetes