用于 .NET Core 的异步跨平台 WebDAV 客户端
Asynchronous cross-platform WebDAV client for .NET Core and other runtimes. It aims to have a full support of RFC4918.
Install WebDav.Client via NuGet.
Install-Package WebDav.ClientFor more information see .NET Standard.
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.
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)
…Authentication using an access token
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", accessToken);
var client = new WebDavClient(httpClient);Authentication using NetworkCredential
var clientParams = new WebDavClientParams
{
BaseAddress = new Uri("http://mywebdav/"),
Credentials = new NetworkCredential("user", "12345")
};
var 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>
{
new KeyValuePair("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;WebDavClient is licensed under the MIT License. See LICENSE.txt for more details.
暂无开放 Issues,或尚未同步最近议题。