WebDAV service opening and client upload, download, delete, new file inventory, list Code (C #)

Source: Internet
Author: User

Enable WebDAV in Windows Server 2003

 

1. Start "IIS manager", select "Web Service extension", and select "WebDAV" to enable the WebDAV function.

 

2. Create a virtual directory corresponding to a local directory.

 

3. Start the "WebClient" service in the system "service ".

 

 

 

 

 

Reference URL

WebDAV documentation rfc2518 http://www.ietf.org/rfc/rfc2518.txt

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

Operate exchange http://www.cnblogs.com/umlchina/archive/2005/04/25/144768.html through WebDAV in C #

 

 

 

// -------------- WebDAV upload code -----

Private void upload ()

{
Httpwebrequest Req = (httpwebrequest) webrequest. Create ("http: // 10.57.144.2/WebDAV/ims ");
Req. Credentials = new networkcredential ("username", "password"); // user name, password // credentialcache. defaultcredentials use the 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 ", 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 method

/* 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 ();*/

}

 

// -------------- Download the WebDAV 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 deletion 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: code used to list folder files -----

Private void webdavlist_click (Object sender, eventargs E)
{

Try
{

Msxml2.xmlhttp30 oxmlhttp = new msxml2.xmlhttp30 (); // you 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/"> ";
Strxml + = "<D: prop> ";
Strxml + = "<D: displayname/>"; // name
Strxml + = "<D: getcontentlength/>"; // size
Strxml + = "<D: iscollection/>"; // whether the folder is used
Strxml + = "<D: getlastmodified/>"; // last modification 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 attributes
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 retrieve the required attributes
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 );
}

}

 

 

// ---- Create a file named 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"); // user name, 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.