c# webdav zugriff

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von BigBen2003.

    c# webdav zugriff

    Editor: Visual Studio 2017 Community

    Hallo,

    für ein Projekt wird ein WebDav Client benötigt, der die Liste aller vorhandenen Dateien auslesen kann.

    Hierzu wurde ein NuGet Paket "Portable WebDav Library" installiert.

    Des weiteren wurden Beispiel-Codes aus der Onlinedokumentation entnommen:
    OnlineDoku: decatec.de/ext/PortableWebDAVL…cd5-9eb6-0850cd041621.htm

    Beim Ausführen der Beispiel-Befehle stoppt das Programm stets an den Befehlen
    - webDavSession.ListAsync
    - webDavClient.PropFindAsync

    Wenn diese Befehle ausgeführt werden sollen, stoppt die Ausführung des Programms gänzlich mit einem Exit Code 0.

    Ein Abfangen von etwaigen Fehlern ist mit try-catch Methoden nicht möglich.

    Im Ausgabefenster wird keine Fehlermeldung ausgegeben.

    Kann jemand einen Tip oder Ratschlag geben?

    Spoiler anzeigen

    C#-Quellcode

    1. static readonly string Username = "username";
    2. static readonly string Password = "password";
    3. static readonly string URL = @"http://www.myserver.com/webdav/";
    4. static readonly string Folder = "/dav/";
    5. static async void WebDavRead()
    6. {
    7. // The base URL of the WebDAV server.
    8. //var webDavServerUrl = "http://www.myserver.com/webdav/";
    9. // Specify the user credentials and use it to create a WebDavSession instance.
    10. var credentials = new NetworkCredential(Username, Password);
    11. var webDavSession = new WebDavSession(URL, credentials);
    12. var propFind = PropFind.CreatePropFindWithEmptyProperties(PropNameConstants.IsHidden, PropNameConstants.DisplayName, PropNameConstants.Name, PropNameConstants.GetContentType, PropNameConstants.CreationDate, PropNameConstants.ResourceType, PropNameConstants.GetLastModified, PropNameConstants.GetContentLength);
    13. var items = await webDavSession.ListAsync(Folder, propFind);
    14. foreach (var item in items)
    15. {
    16. // Handle the response (list of WebDavSessionListItems), e.g item.Uri is the URL of an item (folder or file).
    17. }
    18. try
    19. {
    20. }
    21. catch (Exception ex)
    22. {
    23. Console.WriteLine(ex.Message);
    24. }
    25. }
    26. static async void WebDavTest2()
    27. {
    28. try
    29. {
    30. // The base URL of the WebDAV server.
    31. var webDavServerUrl = URL;
    32. // Specify the user credentials and pass it to a HttpBaseProtocolFilter.
    33. var credentials = new NetworkCredential(Username, Password);
    34. var httpClientHandler = new HttpClientHandler()
    35. {
    36. Credentials = credentials
    37. };
    38. // Use the HttpClientHandler (with credentials) to create a new WebDavClient.
    39. var webDavClient = new WebDavClient(httpClientHandler);
    40. // Create a PropFind object with represents a so called 'allprop' request.
    41. var pf = PropFind.CreatePropFindAllProp();
    42. var response = await webDavClient.PropFindAsync(webDavServerUrl + Folder, WebDavDepthHeaderValue.Infinity, pf);
    43. // You could also use an XML string directly for use with the WebDavClient.
    44. //var xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><D:propfind xmlns:D=\"DAV:\"><D:allprop /></D:propfind>";
    45. //var response = await webDavClient.PropFindAsync(webDavServerUrl + @"/MyFolder/", WebDavDepthHeaderValue.Infinity, xmlString);
    46. // Use the WebDavResponseContentParser to parse the response message and get a MultiStatus instance (this is also an async method).
    47. var multistatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);
    48. // Now you can use the MultiStatus object to get access to the items properties.
    49. foreach (var responseItem in multistatus.Response)
    50. {
    51. // Handle propfind multistatus response, e.g responseItem.Href is the URL of an item (folder or file).
    52. }
    53. // Dispose the WebDavClient when it is not longer needed.
    54. webDavClient.Dispose();
    55. }
    56. catch (Exception ex)
    57. {
    58. Console.WriteLine(ex.Message);
    59. }
    60. finally
    61. {
    62. Console.WriteLine("End");
    63. }
    64. }


    *Topic verschoben*

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Marcus Gräfe“ ()

    Hallo @BigBen2003

    Ich kennte zwar die Lib nicht und habe sie jetzt auf die schnelle auch nicht ausprobiert aber...

    BigBen2003 schrieb:

    Wenn diese Befehle ausgeführt werden sollen, stoppt die Ausführung des Programms gänzlich mit einem Exit Code 0.

    Kannst du das genauer ausführen? Ich gehe von einer Consolenanwendung aus richtig? Es ist also als wenn diese ganz normal "durchläuft" ohne einem Console.Read() oder Console ReadLine() am ende. Meinst du das?

    Wie rufst du denn die Asyncronen Methoden auf. Da kann es nämlich probleme geben wenn diese direkt aus der MAin aufgerufen werden. Ich glaube da was in diese richtig mal aufgeschnappt zu haben.

    Grüße
    Sascha
    If _work = worktype.hard Then Me.Drink(Coffee)
    Seht euch auch meine Tutorialreihe <WPF Lernen/> an oder abonniert meinen YouTube Kanal.

    ## Bitte markiere einen Thread als "Erledigt" wenn deine Frage beantwortet wurde. ##

    Hallo Sascha,

    im Source-Code meinte ich folgende Stellen:

    C#-Quellcode

    1. static readonly string Username = "username";
    2. static readonly string Password = "password";
    3. static readonly string URL = @"http://www.myserver.com/webdav/";
    4. static readonly string Folder = "/dav/";
    5. static async void WebDavRead()
    6. {
    7. // The base URL of the WebDAV server.
    8. //var webDavServerUrl = "http://www.myserver.com/webdav/";
    9. // Specify the user credentials and use it to create a WebDavSession instance.
    10. var credentials = new NetworkCredential(Username, Password);
    11. var webDavSession = new WebDavSession(URL, credentials);
    12. var propFind = PropFind.CreatePropFindWithEmptyProperties(PropNameConstants.IsHidden, PropNameConstants.DisplayName, PropNameConstants.Name, PropNameConstants.GetContentType, PropNameConstants.CreationDate, PropNameConstants.ResourceType, PropNameConstants.GetLastModified, PropNameConstants.GetContentLength);
    13. var items = await webDavSession.ListAsync(Folder, propFind);

    Der Letzte Befehl wird nicht mehr ausgeführt.
    Von diesem Befehl gibt es noch eine andere Variante:

    C#-Quellcode

    1. var items = await webDavSession.ListAsync(Folder);


    Diese unterscheidet sich nur im Fehlen des zweiten Parameters.

    Jedoch wird die Ausführung bei beiden Varianten unterbrochen.

    Das gleiche passiert auch beim letzten Befehl dieses Source-Codes:

    C#-Quellcode

    1. static async void WebDavTest2()
    2. {
    3. try
    4. {
    5. // The base URL of the WebDAV server.
    6. var webDavServerUrl = URL;
    7. // Specify the user credentials and pass it to a HttpBaseProtocolFilter.
    8. var credentials = new NetworkCredential(Username, Password);
    9. var httpClientHandler = new HttpClientHandler()
    10. {
    11. Credentials = credentials
    12. };
    13. // Use the HttpClientHandler (with credentials) to create a new WebDavClient.
    14. var webDavClient = new WebDavClient(httpClientHandler);
    15. // Create a PropFind object with represents a so called 'allprop' request.
    16. var pf = PropFind.CreatePropFindAllProp();
    17. var response = await webDavClient.PropFindAsync(webDavServerUrl + Folder, WebDavDepthHeaderValue.Infinity, pf);


    Von dem Umstand, dass ein Fehler nur dann auftritt, wenn der Aufruf direkt vom Main aus aufgerufen wird, habe ich noch nichts gelesen.
    Hallo

    Ich habe mir gerade die Docu angesehen.
    Ich denke du verwendest die falsche überladung von ListAsync. Laut Doku hier gibt es nur zwei Überladungen welche zwei Parameter entgegennehmen und keinem kann ein Propfind Objekt übergeben werden.

    Das ist das was mir jetzt dazu einfällt. Vieleicht weis ja noch jemand anderes was, da ich kein Webdav habe kann ich es leider nicht testen.

    Grüße
    Sascha
    If _work = worktype.hard Then Me.Drink(Coffee)
    Seht euch auch meine Tutorialreihe <WPF Lernen/> an oder abonniert meinen YouTube Kanal.

    ## Bitte markiere einen Thread als "Erledigt" wenn deine Frage beantwortet wurde. ##

    Hallo Sascha,

    um ganz sicher zu gehen, dass auch die richtigen Überladungen verwendet werden, wurden im Source-Code explizit die volständigen Libraries mit angegeben:

    C#-Quellcode

    1. static async void WebDavRead()
    2. {
    3. var credentials = new System.Net.NetworkCredential(Username, Password);
    4. var webDavSession = new DecaTec.WebDav.WebDavSession(URL, credentials);
    5. IList<DecaTec.WebDav.WebDavSessionItem> items = await webDavSession.ListAsync(Folder);


    Dennoch wird der Befehl "ListAsync" nicht ausgeführt. Bei diesem Befehl wird der ganze Programm mit Exit Code 0 beendet.

    Vielleicht hat jemand anders noch eine Idee, der man nachgehen kann?

    Vielleicht kann die Ursache auch am verwendeten Framework liegen? Es wird das .net Framework 4.7.2 eingesetzt.

    Nachtrag:
    ========

    zwischenzeitlich bin ich einen Schritt weiter gekommen.

    Bislang wurde die Sub direkt aufgerufen. Wenn die Async Sub über einen Task aufgerufen wird, wird der Befehl ListAsync ausgführt.

    Nun wird allerdings vom Webserver ein Multistatus mit http 405 (MethodNotAllowed) zurückgegeben.

    Ich denke, dass in einem der Nextcloud-Foren hierzu eine Lösung stehen wird.

    Ich bedanke mich für die vielseitige Unterstützung.

    Nachtrag:
    ========
    Bei einem erfolgreichen mount-Befehl auf ein webdav Pfad in Nextcloud wird im Protokoll der Webservers zwei "OPTIONS /remote.php/dav/files/[UserName]" Befehle verwarbeitet ehe anschließend die Befehle "PROPFIND /remote.php/dav/files/[UserName]" verarbeitet werden.

    Nachdem die URL angepasst wurde auf "https://[domain]/remote.php/dav/files/[UserName]" wird dennoch keine "OPTIONS Befehle versandt, sondern nur "PROPFIND /remote.php/dav/files/[UserName]".

    Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von „BigBen2003“ ()