Problem mit Auslesen von Ordnern und Datein auf einen FTP

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

Es gibt 28 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Problem mit Auslesen von Ordnern und Datein auf einen FTP

    Hallo ich habe ein Problem und zwar wenn ich mein Programm starte dort dann auf den Download Button gehe bekomme ich folge Fehlermeldung:

    Der Remote Server hat einen Fehler zurückgegeben (501) nicht implementiert

    hier einmal der Code:

    C#-Quellcode

    1. void DownloadFtpDirectory(string url, string localPath)
    2. {
    3. try
    4. {
    5. HttpWebRequest listRequest = (HttpWebRequest)WebRequest.Create(url);
    6. listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    7. List<string> lines = new List<string>();
    8. using (HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse())
    9. using (Stream listStream = listResponse.GetResponseStream())
    10. using (StreamReader listReader = new StreamReader(listStream))
    11. {
    12. while (!listReader.EndOfStream)
    13. {
    14. lines.Add(listReader.ReadLine());
    15. }
    16. }
    17. foreach (string line in lines)
    18. {
    19. string[] tokens =
    20. line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
    21. string name = tokens[8];
    22. string permissions = tokens[0];
    23. string localFilePath = Path.Combine(localPath, name);
    24. string fileUrl = url + name;
    25. if (permissions[0] == 'd')
    26. {
    27. if (!Directory.Exists(localFilePath))
    28. {
    29. Directory.CreateDirectory(localFilePath);
    30. }
    31. DownloadFtpDirectory(fileUrl + "/", localFilePath);
    32. }
    33. else
    34. {
    35. HttpWebRequest downloadRequest = (HttpWebRequest)WebRequest.Create(fileUrl);
    36. downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    37. using (HttpWebResponse downloadResponse =
    38. (HttpWebResponse)downloadRequest.GetResponse())
    39. using (Stream sourceStream = downloadResponse.GetResponseStream())
    40. using (Stream targetStream = File.Create(localFilePath))
    41. {
    42. byte[] buffer = new byte[10240];
    43. int read;
    44. while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
    45. {
    46. targetStream.Write(buffer, 0, read);
    47. }
    48. }
    49. }
    50. }
    51. }
    52. catch (Exception ex)
    53. {
    54. MessageBox.Show(ex.Message);
    55. }
    56. }


    *Topic verschoben*

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

    das ist bei Zeile 11 der Fehler

    //edit
    hier nochmal komplette fehlermeldung:

    Eine nicht behandelte Ausnahme des Typs "System.Net.WebException" ist in System.dll aufgetreten.

    Zusätzliche Informationen: Der Remoteserver hat einen Fehler zurückgegeben: (501) Nicht implementiert.
    Das kann ich von hier aus schlecht beurteilen - ohne ihn zu kennen.

    Ah!

    C#-Quellcode

    1. HttpWebRequest listRequest = (HttpWebRequest)WebRequest.Create(url);
    2. listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    Du erstellst einen HttpWebRequest, stellst aber eine Ftp-Methode ein - das passt glaub nicht zusammen: Http <> Ftp

    Aber trotzdem täte mich auch die "eigentlich richtige" url interessieren - wie lautet die?
    tja, weiss ich auch nicht. Sieht im Browser aus wie ein Ftp-Listing.
    Aber weils im Browser ist, ists ja eiglich Http - oder mein Browser macht da eigenständig was, wozu er eiglich nicht da ist - (Programme wollen ja heutzutage immer schlauer sein als ihre Benutzer)
    Probier das mal einfach mit WebClient.DownloadString(url) zu holen.

    Oder noch weniger Änderung wäre, wenn du zunächstmal mit WebRequestMethods.Http.Get probierst.
    Die Frage verstehe ich nicht recht.
    Üblicherweise meine ich, was ich schreibe, so wie ichs schreibe - insofern weiss ich nicht inwiefern ich iwas sonst gemeint haben könnte.

    ErfinderDesRades schrieb:

    Probier das mal einfach mit WebClient.DownloadString(url) zu holen.

    Oder noch weniger Änderung wäre, wenn du zunächstmal mit WebRequestMethods.Http.Get probierst.
    Ist das iwie nicht verständlich, oder uneindeutig?
    Ehm ja habs ^^

    aber jetzt hab ich wieder nen Problem hier einmal der aktuelle:

    C#-Quellcode

    1. void DownloadFtpDirectory(string url, string localPath)
    2. {
    3. HttpWebRequest listRequest = (HttpWebRequest)WebRequest.Create(url);
    4. listRequest.Method = WebRequestMethods.Http.Get;
    5. List<string> lines = new List<string>();
    6. using (HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse())
    7. using (Stream listStream = listResponse.GetResponseStream())
    8. using (StreamReader listReader = new StreamReader(listStream))
    9. {
    10. while (!listReader.EndOfStream)
    11. {
    12. lines.Add(listReader.ReadLine());
    13. }
    14. }
    15. foreach (string line in lines)
    16. {
    17. string[] tokens =
    18. line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
    19. string name = tokens[8]; //hier
    20. string permissions = tokens[0];
    21. string localfile = Path.Combine(sPfad, name);
    22. string sDAce = url + name;
    23. if (permissions[0] == 'd')
    24. {
    25. if (!Directory.Exists(localfile))
    26. {
    27. Directory.CreateDirectory(localfile);
    28. }
    29. DownloadFtpDirectory(sDAce + "/", localfile);
    30. }
    31. else
    32. {
    33. HttpWebRequest downloadRequest = (HttpWebRequest)WebRequest.Create(sDAce);
    34. downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    35. using (HttpWebResponse downloadResponse =
    36. (HttpWebResponse)downloadRequest.GetResponse())
    37. using (Stream sourceStream = downloadResponse.GetResponseStream())
    38. using (Stream targetStream = File.Create(localfile))
    39. {
    40. byte[] buffer = new byte[10240];
    41. int read;
    42. while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
    43. {
    44. targetStream.Write(buffer, 0, read);
    45. }
    46. }
    47. }
    48. }
    49. }


    Fehlermeldung:

    Eine nicht behandelte Ausnahme des Typs "System.IndexOutOfRangeException" ist in 85th Operation Command Launcher.exe aufgetreten.

    Zusätzliche Informationen: Der Index war außerhalb des Arraybereichs.
    Nein keine ahnung ich habe den code aus dem Internet und ein wenig an mein code angepasst.

    ich habe aber ein wenig rumprobiert

    C#-Quellcode

    1. string[] tokens = new string[9];
    2. line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
    3. string name = tokens[8];
    4. string permissions = tokens[0];
    5. string localfile = Path.Combine(sPfad, name);
    6. string sDAce = url + name;


    ich weiss nicht ob ich das so richtig gemacht habe aber ich bekomme wieder einen neuen Fehler

    Eine nicht behandelte Ausnahme des Typs "System.ArgumentNullException" ist in mscorlib.dll aufgetreten.

    Zusätzliche Informationen: Der Wert darf nicht NULL sein.

    Zeile 6
    Jo, also was der Server liefert ist dieses html:

    XML-Quellcode

    1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    2. <html>
    3. <head>
    4. <title>Index of /</title>
    5. </head>
    6. <body>
    7. <h1>Index of /</h1>
    8. <table>
    9. <tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr>
    10. <tr><th colspan="5"><hr></th></tr>
    11. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@ASR_AI3/">@ASR_AI3/</a></td><td align="right">2016-12-14 03:05 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    12. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@CBA_A3/">@CBA_A3/</a></td><td align="right">2017-03-01 17:37 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    13. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@CUP_Terrains/">@CUP_Terrains/</a></td><td align="right">2017-02-03 15:20 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    14. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@ProjectOPFOR/">@ProjectOPFOR/</a></td><td align="right">2017-02-06 18:39 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    15. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@RHSAFRF/">@RHSAFRF/</a></td><td align="right">2017-02-20 18:12 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    16. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@RHSUSF/">@RHSUSF/</a></td><td align="right">2017-02-20 18:16 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    17. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@USAF/">@USAF/</a></td><td align="right">2016-11-25 21:51 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    18. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@ace/">@ace/</a></td><td align="right">2017-03-21 23:32 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    19. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@acex/">@acex/</a></td><td align="right">2016-11-25 20:54 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    20. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@bwa3/">@bwa3/</a></td><td align="right">2017-04-02 23:43 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    21. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@maps/">@maps/</a></td><td align="right">2015-12-30 17:51 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    22. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@mcc_sandbox/">@mcc_sandbox/</a></td><td align="right">2016-11-25 21:08 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    23. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@mods/">@mods/</a></td><td align="right">2015-12-30 16:52 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    24. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@task_force_radio/">@task_force_radio/</a></td><td align="right">2017-04-03 16:51 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    25. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="@unsung/">@unsung/</a></td><td align="right">2016-12-16 21:56 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    26. <tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="userconfig/">userconfig/</a></td><td align="right">2016-12-14 02:51 </td><td align="right"> - </td><td>&nbsp;</td></tr>
    27. <tr><th colspan="5"><hr></th></tr>
    28. </table>
    29. <address>Apache/2.4.10 (Debian) Server at arma3dl2.85th-operations-command.de Port 80</address>
    30. </body></html>
    Da musste iwie deine Infos rauspicken