Giter Site home page Giter Site logo

stefh / webdav-client Goto Github PK

View Code? Open in Web Editor NEW
14.0 4.0 5.0 2.19 MB

An easy-to-use async WebDAV client for .NETStandard, Portable and .NET

License: MIT License

C# 99.91% Batchfile 0.09%
webdav-client portable async-webdav-client netstandard netcore webdav

webdav-client's Introduction

WebDAV-Client

An easy-to-use async WebDAV client for .NET, .NETStandard, UAP and Portable based on https://github.com/skazantsev/WebDavClient.

NuGet Badge

Supported Frameworks

  • .NET Framework 4.5 and 4.6
  • .NET Standard 1.1, 2.0 and 2.1
  • .NET 6.0 and 7.0
  • Blazor WASM
  • Portable (Profile7, Profile32, Profile44, Profile111, Profile115). See portablelibraryprofiles.

Basic usage

using (var webDavClient = new WebDavClient())
{
    var result = await webDavClient.Propfind("http://mywebdav/1.txt");
    if (result.IsSuccessful)
        // continue ...
    else
        // handle an error
}

Usage in Blazor WASM

See https://github.com/StefH/WebDAV-AudioPlayer/tree/master/src/Blazor-WebDAV-AudioPlayer.v2

Using BaseAddress

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

Operations with files and directories (resources & collections)

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

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

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

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

    await webDavClient.GetRawFile("file.txt"); // get a file without processing from the server

    await webDavClient.GetProcessedFile("file.txt"); // get a file that can be processed by the server

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

PROPFIND example

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

Authentication example

var clientParams = new WebDavClientParams
{
    BaseAddress = new Uri("http://mywebdav/"),
    Credentials = new NetworkCredential("user", "12345")
};
using (var webDavClient = new WebDavClient(clientParams))
{
    // call webdav methods...
}

License

WebDAV-Client is licensed under the MIT License.

webdav-client's People

Contributors

dependabot[bot] avatar stefh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

webdav-client's Issues

Authentication example not work in blazor wasm

Authentication example not work in blazor wasm.

var clientParams = new WebDavClientParams
{
    BaseAddress = uri,
    Credentials = new NetworkCredential(userName, password)
};

return new WebDavClient(clientParams);

PlatformNotSupportedException on Blazor Webassembly / WASM

Hello,
I'm trying to get the Webdav client running on a .NET 6.0 blazor client app.
I am getting the mentioned exception on the Constructor of WebDavClient, saying 'Operation is not supported on this platform.'

I guess I have to add some switches to tell the library to use the browsers' httpClient.
Maybe the ones you gave in WebDAV-Client-Solution.sln.DotSettings. But don't know how?
Best regards
Oliver

Nextcloud WebDav integration

I'm trying to get it working with authentication in mind but can't make it work in any way(I get back "Conflict" or "Not found" with different paths, not quite useful for debugging). Is there any guide on how to make proper calls?

And the base address should reflect the folder which you want to find/upload files or the base address should just refer to the base root path for a certain user? (in my case Nextcloud gives me the WebDAV path https://<DOMAIN>/remote.php/dav/files/<USER>/)

CORS Error

Following up on the simple test:

httpClient.BaseAddress = new Uri("http://localhost:8080/");
using var client = new WebDav.WebDavClient(httpClient);
var result = await client.Propfind("/");

results in a CORS exception:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: TypeError: NetworkError when attempting to fetch resource.
System.Net.Http.HttpRequestException: TypeError: NetworkError when attempting to fetch resource.
 ---> TypeError: NetworkError when attempting to fetch resource.
   --- End of inner exception stack trace ---
   at System.Net.Http.BrowserHttpInterop.<CancelationHelper>d__13`1[[System.Runtime.InteropServices.JavaScript.JSObject, System.Runtime.InteropServices.JavaScript, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]].MoveNext()
   at System.Net.Http.BrowserHttpHandler.CallFetch(HttpRequestMessage request, CancellationToken cancellationToken, Nullable`1 allowAutoRedirect)
   at System.Net.Http.BrowserHttpHandler.<SendAsync>g__Impl|53_0(HttpRequestMessage request, CancellationToken cancellationToken, Nullable`1 allowAutoRedirect)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at WebDav.Infrastructure.WebDavDispatcher.Send(Uri requestUri, HttpMethod method, RequestParameters requestParams, CancellationToken cancellationToken)
   at WebDav.WebDavClient.Propfind(Uri requestUri, PropfindParameters parameters)
   at Cashier.Components.Pages.WebDAVDemo.OnClientLs() in D:\src\cashier-blazor\src\Cashier\Components\Pages\WebDAVDemo.razor:line 56
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at MudBlazor.MudBaseButton.OnClickHandler(MouseEventArgs ev)
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

However, CORS access is enabled. I'm using rclone with --allow-origin "*" and can confirm that issuing OPTIONS and PROPFIND HTTP commands works with the same HttpClient.

Also, if I try to mix using the HttpClient directly and then using the WebDAV client to which the HttpClient has been passed in the constructor, it throws a different exception.

`Operation is not supported on this platform` Exception in Blazor

Trying an extremely simple example in Blazor:

using var client = new WebDav.WebDavClient();
var result = await client.Propfind("/");

results in an exception:

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Operation is not supported on this platform.
System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Net.Http.BrowserHttpHandler.set_Credentials(ICredentials value)
   at System.Net.Http.HttpClientHandler.set_UseDefaultCredentials(Boolean value)
   at WebDav.WebDavClient.ConfigureHttpClient(WebDavClientParams params)
   at WebDav.WebDavClient..ctor(WebDavClientParams params)
   at WebDav.WebDavClient..ctor()
   at Cashier.Components.Pages.WebDAVDemo.OnClientLs() in D:\src\cashier-blazor\src\Cashier\Components\Pages\WebDAVDemo.razor:line 54
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at MudBlazor.MudBaseButton.OnClickHandler(MouseEventArgs ev)
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)

Need to initialize WebDavClient with two parameters

Hi Stef,
thanks for your new release 1.1.2, I'm making progress.
Passing the HttpClient with the constructor, the former EPlatformNotSupportedException is gone.
But I need to pass the WebDavClientParams also, because I see no other way to pass BaseAddress and Credentials.

So I need a constructor:
public WebDavClient(HttpClient httpClient, WebDavClientParams @params)
or a similiar way (property etc).
Your method "ConfigureHttpClient" is private, so I cannot call it.

Best regards!
Oliver

Add support for using a custom HttpClient or HttpMessageHandler

It would be useful when you would be able to provide a custom HttpClient or HttpMessageHandler. This would allow the usage of the client in the following scenarios:

  • Test for ASP.NET Core using the TestHost (with custom HttpMessageHandler or HttpClient)
  • Usage on Android and iOS (with custom HttpMessageHandler)

Feedback

Hi you asked me for feedback for the library,

I will only be able to test it properly, here only the feedback I noticed so far with a quick look over:

First of all really nice, that you have explicit Blazor support and have written lots of tests.

  • nuget package. You don't have a nuget package yet? That makes it harder to get started with the library or even find it.

  • Windows Authentification: To use Windows Authentification/NTLM or MS-OFBA:
    I've seen you activate UseDefaultAuthentification by default that is nice. Maybe a example
    for Windows Authentification and MS-OFBA?
    https://www.webdavsystem.com/server/documentation/choosing_authentication/

  • PropFind Instead of Propfind
    Since PROPFIND means Property Find I would have written PropFind. But I have no strong opinon on that, since both ways to write kinda look wrong.

  • Async Postfixes:
    Again Personal Style, I write the async method names always with Async postfix but both sides have good arguments and I also experimented without the Async Postfix. (Especially if a class has async methods and non async methods)

  • [NotNull] Arguments - again very nice that you use Nullable types, big fan! But why you used the [NonNull]-Attribute - are there issues with .NET Framework if you use the ? syntax? Or is that for legacy reasons?

  • Everywhere Argument Validation I like that! Also the documenation for all methods.

  • Idea: A way to call custom methods? Like to access the WebDav Dispatcher?
    Microsoft has some custom protcol extensions for web dav.
    But I mean I could create one and pass it in the constructor.

Greetings
Michael

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.