zoukankan      html  css  js  c++  java
  • webDAV服务的开启以及客户端的上传、下载、删除、新建文件夾、列表的代码(C#)

     

    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 

    在C#中通过webdav操作exchange    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());
                }            
            }

  • 相关阅读:
    通过python来获取网页状态
    php多域名跳转nginx
    mybatis-plus主键策略
    mybatis-plus ActiveRecord模式
    mybatis-plus-Cud操作
    mybatis-plus高级操作
    mybatis-plus入门
    ☕【Java技术指南】「序列化系列」深入挖掘FST快速序列化压缩内存的利器的特性和原理
    虚拟机研究系列-「GC本质底层机制」SafePoint的深入分析和底层原理探究指南
    👊 Spring技术原理系列(7)带你看看那些可能你还不知道的Spring特性技巧哦!
  • 原文地址:https://www.cnblogs.com/xgjblog/p/3829696.html
Copyright © 2011-2022 走看看