WebDav.Client

An easy-to-use async WebDAV client for .NET Standard


Keywords
WebDAV, WebDAVClient, WebDAV.Client, c-sharp, dotnet, dotnet-core, dotnet-standard, webdav-client
License
MIT
Install
Install-Package WebDav.Client -Version 1.1.0

Documentation

WebDAV .NET client Build status

Asynchronous cross-platform WebDAV client for .NET Standard. It aims to have a full support of RFC4918.

Installation

Install WebDav.Client via NuGet.

Install-Package WebDav.Client

Supported platforms

  • .NET Framework 4.5+
  • .NET Core
  • Mono
  • Xamarin
  • UWP

For more information see .NET Standard.

Usage notes

WebDavClient uses HttpClient under the hood that is why it is a good practice to share a single instance for the lifetime of the application.

If you use a dependency injection container to manage dependencies it is a good practice to register WebDavClient as a singleton.

It's also possible to instantiate WebDavClient with a pre-configured instance of HttpClient.

When using GetRawFile / GetProcessedFile don't forget to dispose the response.

Usage examples

Basic usage:

class Example
{
    public static IWebDavClient _client = new WebDavClient();

    public void MakeCalls()
    {
        var result = await _client.Propfind("http://mywebdav/1.txt");
        if (result.IsSuccessful)
            // continue ...
        else
            // handle an error
    }
}

Using BaseAddress:

var clientParams = new WebDavClientParams { BaseAddress = new Uri("http://mywebdav/") };
using (var client = new WebDavClient(clientParams))
{
    await client.Propfind("1.txt");
}

Operations with files and directories (resources & collections):

var clientParams = new WebDavClientParams { BaseAddress = new Uri("http://mywebdav/") };
using (var client = new WebDavClient(clientParams))
{
    await client.Mkcol("mydir"); // create a directory

    await client.Copy("source.txt", "dest.txt"); // copy a file

    await client.Move("source.txt", "dest.txt"); // move a file

    await client.Delete("file.txt", "dest.txt"); // delete a file

    using (var response = await client.GetRawFile("file.txt")) // get a file without processing from the server
    {
        // use response.Stream
    }

    using (var response = await client.GetProcessedFile("file.txt")) // get a file that can be processed by the server
    {
        // use response.Stream
    }

    await client.PutFile("file.xml", File.OpenRead("file.xml")); // upload a resource
}

Authentication:

var clientParams = new WebDavClientParams
{
    BaseAddress = new Uri("http://mywebdav/"),
    Credentials = new NetworkCredential("user", "12345")
};
_client = new WebDavClient(clientParams);

PROPFIND example:

// list files & subdirectories in 'mydir'
var result = await _client.Propfind("http://mywebdav/mydir");
if (result.IsSuccessful)
{
    foreach (var res in result.Resources)
    {
        Trace.WriteLine("Name: " + res.DisplayName);
        Trace.WriteLine("Is directory: " + res.IsCollection);
        // etc.
    }
}

PROPFIND with custom properties:

var propfindParams = new PropfindParameters
{
    Namespaces = new [] { new NamespaceAttr("myns", "https://example.com/") },
    CustomProperties = new [] { XName.Get("myprop", "https://example.com/") }
};
var result = await client.Propfind("http://mywebdav/mydir", propfindParams);

Custom headers:

var propfindParams = new PropfindParameters
{
    Headers = new List<KeyValuePair<string, string>>
    {
        new KeyValuePair<string, string>("User-Agent", "Not a browser")
    }
};
var result = await _client.Propfind("http://mywebdav/1.txt", propfindParams);

Content-Range or other content headers:

// Content headers need to be set directly on HttpContent instance.
var content = new StreamContent(File.OpenRead("test.txt"));
content.Headers.ContentRange = new ContentRangeHeaderValue(0, 2);
var result = await _client.PutFile("http://mywebdav/1.txt", content);

Synchronous API:

  // will block the current thread, so use it cautiously
  var result = _client.Propfind("1.txt").Result;

License

WebDavClient is licensed under the MIT License. See LICENSE.txt for more details.