Sagui Itay - Unity Assets, software development and mobile games

Best C# client for WebDAV

I always prefer using existing libraries implemented by someone else – it means that I don’t have to study the subject matter, and not less important, that someone else maintains the code for me (tests the code, implements new features, follow-ups on newer versions of the APIs if any, etc.).

That being said, every now and then I find myself searching for such solutions (whether it’s in GitHub, Google Code, or perhaps just a blog post), and not finding anything useful, even for basic requirements. Case in point – I needed a C# WebDAV client. Async/Await support was not a requirement but preferred.

I’ve found several libraries which provided WebDAV support, most didn’t support async/await out of the box, or were not strongly-typed.

I ended up writing my own library, with the following features:

  • Available as a NuGet packages
  • Fully support Async/Await
  • Strong-typed
  • Implemented using HttpClient, which means support for extendibility such as throttling and monitoring
  • Supports Unauthenticated or Windows Authentication-based access
  • Supports custom Certificate validation
  • Supports the full WebDAV API:
    • Retrieving files & folders
    • Listing items in a folder
    • Creating & deleting folders
    • Downloading & uploading files
    • Downloading & uploading partial content
    • Moving & copying files and folders

Here’s the sample code taken from the GitHub repository:

// Basic authentication required
var client = new WebDAVClient.Client(new NetworkCredential
{ 
  UserName = "USERNAME", 
  Password = "PASSWORD"
});
// OR without authentication
var client = new WebDAVClient.Client(new NetworkCredential());

// Set basic information for WebDAV provider
client.Server = "https://webdav.4shared.com";
client.BasePath = "/";

// Retrieve list of items in root folder
var files = await client.List();

// Find folder named 'Test'
var folder = files.FirstOrDefault(f => f.Href.EndsWith("/Test/"));

// Reload folder 'Test'
var folderReloaded = await client.Get(folder.Href);

// Retrieve list of items in 'Test' folder
var folderFiles = await client.List(folder.Href);

// Find first file in 'Test' folder
var folderFile = folderFiles.FirstOrDefault(f => f.IsCollection == false);
// Download file into a Stream
var stream = await client.Download(folderFile.Href);

// Upload a file (with a random name) to 'Test' folder
var tempName = Path.GetRandomFileName();
var fileUploaded = await client.Upload(folder.Href, File.OpenRead(@"...path-to-file..."), tempName);

// Create a folder (with a random name) in the 'Test' folder
tempName = Path.GetRandomFileName();
var folderCreated = await client.CreateDir(folder.Href, tempName);

What is WebDAV?

Web Distributed Authoring and Versioning (WebDAV) is a protocol that provides a framework for users to create, change and move documents on a server.

The most important features include the properties about an author or modification date, collections, and overwrite protection.
WebDAV extends the Hypertext Transfer Protocol (HTTP) and takes advantage of existing technologies such as Transport Layer Security (TLS), digest access authentication and XML.

WebDAV

What’s included in the WebDAV protocol?

WebDAV extends the standard HTTP verbs (GET, POST, PUT, DELETE, PATCH), and adds the following verbs:

  • COPY: copy a resource from one uniform resource identifier (URI) to another
  • LOCK: put a lock on a resource. WebDAV supports both shared and exclusive locks.
  • MKCOL: create collections (also known as a directory)
  • MOVE: move a resource from one URI to another
  • PROPFIND: retrieve properties, stored as XML, from a web resource. It is also overloaded to allow one to retrieve the collection structure (also known as directory hierarchy) of a remote system.
  • PROPPATCH: change and delete multiple properties on a resource in a single atomic act
  • UNLOCK: remove a lock from a resource

The PROPFIND and PROPPATCH verbs facilitate the retrieval and update of items’ properties, which are represented as key/value pairs, with the key being a Uniform Resource Identifier (URI) and the values being XML elements.

What are WebDAV clients?

Clients are tools or programs that can open a WebDAV connection to a server using the WebDAV protocol. Windows WebDAV clients include the Windows operating system itself, as well as Microsoft Office (both of which provide native support), while Linux has clients such as GNOME Files and Konqueror, and macOS supports WebDAV natively.

Where can WebDAV be used?

There are many servers and online services that provide WebDAV support. These include:

  • Google Drive
  • Box (but as of Oct. 25, 2019, Box ended official WebDAV support)
  • Yandex
  • Nextcloud and ownCloud
  • 4Shared
  • DriveHQ
  • HiDisk
  • Jungle Disk
  • pCloud

What about programmatic access?

In order to connect to a WebDAV server using C#, you can use my WebDAV Client library, available as a Nuget package, and open-sourced in GitHub.

In addition, I provide a Unity asset for WebDAV access, based on the above library, which is available in the Unity Asset Store.

Other languages have their own implementation, such as Sardine for Java.

Is WebDAV obsolete?

While WebDAV support first appeared in 1996, and was never extremely popular, WebDAV remains a common protocol, and is supported by many web servers, including Apache, Nginx and Microsoft’s IIS, which can operate as WebDAV servers (either natively, or using an extension module).
In addition, WebDAV serves as the basis for a few additional protocols, such as CalDAV (for managing calendars), GroupDAV (for managing groups and calendars) and CardDAV (for managing contacts)

Is WebDAV safe?

WebDAV is a protocol that defines the HTTP verbs and headers. It takes advantage of other web technologies, such as TLS and digest access authentication to provide the required security layer. This makes WebDAV safe and secure, just like HTTPS.

What are the main alternatives for WebDAV?

Since WebDAV provides basic file management capabilities, there are quite a few alternatives. The main WebDAV alternatives include:

  • File Transfer Protocol (FTP) which is a simple and widely adapted protocol that allows users to transfer files between network hosts.
  • SSH File Transfer Protocol (SFTP) is an extension of the Secure Shell protocol (SSH) that provides secure file-transfer capability.
  • The Server Message Block (SMB) protocol allows Microsoft Windows and open-source Samba clients to access and manage files and folders remotely.
  • And lastly, AtomPub is an HTTP-based protocol for creating and updating web resources.