The opening of the WebDAV service and the code for the client to upload, download, delete, create a new folder, List (C #)

Source: Internet
Author: User

Windows Server 2003 opens the WebDAV service

1. Start the IIS Manager Select Web Service extensions, select the Allow button for WebDAV to start the WebDAV feature

2. Create a virtual directory that corresponds to a local directory.

3, start the "WebClient" service in the system "service"

Reference URL

WebDAV document RFC2518 Http://www.ietf.org/rfc/rfc2518.txt

Summary of common methods and concepts of WebDAV http://blog.csdn.net/mahongming/archive/2007/09/10/1779033.aspx

to manipulate Exchange http://www.cnblogs.com/umlchina/archive/2005/04/25/144768.html in C # through WebDAV

--------------WebDAV Upload Code-----

private void Upload ()

{
HttpWebRequest req = (HttpWebRequest) webrequest.create ("Http://10.57.144.2/WebDAV/IMS for external Education Training Pptnew.pdf");
Req. Credentials = new NetworkCredential ("username", "password");//user name, password//credentialcache.defaultcredentials Use default authentication
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 Training for external education 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 ();

You can also use the following methods

/* 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 download Code-----

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 Delete Code-----

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 lists the folder file list code-----

private void Webdavlist_click (object sender, EventArgs e)
{

Try
{

MSXML2. XMLHTTP30 oxmlhttp = new MSXML2.    XMLHTTP30 (); Need to add 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/>"; Name
Strxml + = "<d:getcontentlength/>"; Size
Strxml + = "<d:iscollection/>"; Whether the folder
Strxml + = "<d:getlastmodified/>"; Last Modified Time
Strxml + = "</d:prop>";
Strxml + = "</d:propfind>";

/*strxml = "<?xml version=/" 1.0/"encoding=/" utf-8/"?>";
Strxml + = "<d:propfind xmlns:d=/" dav:/">";
Strxml + = "<D:allprop/>"; List all the properties
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));

Parse the returned XML file to remove the desired attribute
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);
}

}

----New Folder----

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");//username, password
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 ());
           }             
       }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.