• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

webDAV服务的开启以及客户端的上传、下载、删除、新建文件夾、列表的代码(C#) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

windows server 2003开启webDAV服务

 

1、 启动“IIS管理器”选择“WEB服务扩展”,选择“WEBDAV”的允许按钮启动WEBDAV功能

 

2、建立一个虚拟目录,对应到一个本地目录。

 

3、启动系统“服务”中的“WebClient”服务

 

 

 

 

 

参考网址

WebDAV文档rfc2518    http://www.ietf.org/rfc/rfc2518.txt

webdav常用方法和概念总结   http://blog.csdn.net/mahongming/archive/2007/09/10/1779033.aspx 

http://www.cnblogs.com/umlchina/archive/2005/04/25/144768.html

 

 

 

        

      //--------------WebDAV上传代码-----

        private void upload()

        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://10.57.144.2/WebDAV/IMS對外教育訓練PPTnew.pdf");
            req.Credentials = new NetworkCredential("username", "password");//用户名,密码   //CredentialCache.DefaultCredentials使用默认的认证
            req.PreAuthenticate = true;
            req.Method = "PUT";
            req.AllowWriteStreamBuffering = true;

            // Retrieve request stream
            Stream reqStream = req.GetRequestStream();

            // Open the local file
            FileStream rdr = new FileStream("C://IMS對外教育訓練PPTnew.pdf", FileMode.Open);

            // Allocate byte buffer to hold file contents
            byte[] inData = new byte[4096];

            // loop through the local file reading each data block
            //  and writing to the request stream buffer
            int bytesRead = rdr.Read(inData, 0, inData.Length);
            while (bytesRead > 0)
            {
                reqStream.Write(inData, 0, bytesRead);
                bytesRead = rdr.Read(inData, 0, inData.Length);
            }

            rdr.Close();
            reqStream.Close();

            req.GetResponse();

 

             //也可以用以下的方式

            /*  System.Uri myURi = new System.Uri("http://10.57.144.2/WebDAV/hello.doc");
              FileStream inStream = File.OpenRead("C://timeTest.doc"); 
 
            WebRequest req = WebRequest.Create(myURi);
            req.Method = "PUT";
              req.Timeout = System.Threading.Timeout.Infinite;
 
              req.Credentials = CredentialCache.DefaultCredentials;
              Stream outStream = req.GetRequestStream();
 
              //CopyStream(inStream, outStream);
              byte[] inData = new byte[4096];
              int bytesRead = inStream.Read(inData, 0, inData.Length);
              while (bytesRead > 0)
              {
                  outStream.Write(inData, 0, bytesRead);
                  bytesRead = inStream.Read(inData, 0, inData.Length);
              }
              inStream.Close();
              outStream.Close();
              req.GetResponse();*/

        }

 

        //--------------WebDAV下载代码-----

        private void WebDAVGet_Click(object sender, EventArgs e)
        {
            System.Uri myURi = new System.Uri(@"http://10.57.144.2/WebDAV/hello.doc");
            string sfilePath = "C://hello.doc";

            WebRequest req = WebRequest.Create(myURi);
            req.Method = "GET";
            req.Timeout = System.Threading.Timeout.Infinite;
            req.Credentials = CredentialCache.DefaultCredentials;
            WebResponse res = req.GetResponse();
            Stream inStream = res.GetResponseStream();

            FileStream fs = new FileStream(sfilePath, FileMode.OpenOrCreate);

            byte[] inData = new byte[4096];
            int bytesRead = inStream.Read(inData, 0, inData.Length);
            while (bytesRead > 0)
            {
                fs.Write(inData, 0, bytesRead);
                bytesRead = inStream.Read(inData, 0, inData.Length);
            }

            fs.Close();

            inStream.Close();
        }

 

 

       //--------------WebDAV删除代码-----

        private void WebDAVDel_Click(object sender, EventArgs e)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"http://10.57.144.2/WebDAV/Powerpoint.pptx");
            //req.Credentials = new NetworkCredential("Administrator", "123456");
            req.PreAuthenticate = true;
            req.Method = "DELETE";
            //req.AllowWriteStreamBuffering = true;

            req.GetResponse();
        }

 

 

        //--------------WebDAV列出文件夹文件列表代码-----

        private void WebDAVList_Click(object sender, EventArgs e)
        {
            
            try
            {

                MSXML2.XMLHTTP30 oXMLHttp = new MSXML2.XMLHTTP30();    //需要增加Microsoft.XML 6.0  COM

                String sUrl = "http://10.57.144.2/WebDAV/";

                string strXml;
                strXml = "<?xml version=/"1.0/"?> ";
                strXml += "<d:propfind xmlns:d=/"DAV:/" xmlns:o=/"urn:schemas-microsoft-com:office:office/">";
                strXml += "<d:prop>";
                strXml += "<d:displayname/>";       //名称
                strXml += "<d:getcontentlength/>";    //大小
                strXml += "<d:iscollection/>";     //是否文件夹
                strXml += "<d:getlastmodified/>";   //最后修改时间
                strXml += "</d:prop>";
                strXml += "</d:propfind>";

 

                /*strXml = "<?xml version=/"1.0/" encoding=/"utf-8/" ?>";
                strXml += "<D:propfind xmlns:D=/"DAV:/">";
                strXml += "<D:allprop/>";     //列出所有的属性
                strXml += "</D:propfind>";*/

 

 

                oXMLHttp.open("PROPFIND", sUrl, false, "administrator", "drm.123");

                oXMLHttp.setRequestHeader("Content-Type", "text/xml");
                oXMLHttp.setRequestHeader("Translate", "f");

                //Console.WriteLine(sQuery.Length);
                Console.WriteLine(strXml);
                oXMLHttp.send(strXml);

                Console.WriteLine(oXMLHttp.status);
                Console.WriteLine(oXMLHttp.statusText);
                File.WriteAllBytes(@"C:/list.xml", Encoding.UTF8.GetBytes(oXMLHttp.responseText));

 

 

               //对返回的xml文件进行解析,取出所需属性
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(oXMLHttp.responseText);


                XmlNode root = doc.DocumentElement;
                System.Collections.IEnumerator ienum = root.GetEnumerator();
                XmlNode book;
                while (ienum.MoveNext())
                {
                    book = (XmlNode)ienum.Current;
                    book = book.ChildNodes[1].ChildNodes[1];
                    System.Collections.IEnumerator ienum2 = book.GetEnumerator();
                    while (ienum2.MoveNext())
                    {
                        XmlNode node = (XmlNode)ienum2.Current;
                        Console.Write(node.InnerXml);
                        Console.Write("-");
                    }
                    //Console.WriteLine(book.OuterXml);
                    //Console.WriteLine(book.InnerXml);
                    Console.WriteLine();
                }


            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
            }

        }

 

 

      //----新建文件夾----

       private void WebDAVNewFolder_Click(object sender, EventArgs e)
        {
            try
            {
                // Create the HttpWebRequest object.
                HttpWebRequest objRequest = (HttpWebRequest)HttpWebRequest.Create(@"http://10.57.144.2/WebDAV/new");
                // Add the network credentials to the request.
                objRequest.Credentials = new NetworkCredential("F3226142", "drm.123");//用户名,密码
                // Specify the method.
                objRequest.Method = "MKCOL";

                HttpWebResponse objResponse = (System.Net.HttpWebResponse)objRequest.GetResponse();

                // Close the HttpWebResponse object.
                objResponse.Close();

            }
            catch (Exception ex)
            {
                throw new Exception("Can't create the foder" + ex.ToString());
            }            
        }


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#AutoMapper的简单扩展发布时间:2022-07-10
下一篇:
【疯了C#】神奇的换肤(二)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap