Obsługa FTP - błąd

Obsługa FTP - błąd
HI
  • Rejestracja:prawie 13 lat
  • Ostatnio:dzień
  • Postów:1857
0

Witam.

Znalazłem w sieci klasę do obsługi ftp :

Kopiuj
class ftp
    {
        private string host = null;
        private string user = null;
        private string pass = null;
        private FtpWebRequest ftpRequest = null;
        private FtpWebResponse ftpResponse = null;
        private Stream ftpStream = null;
        private int bufferSize = 2048;

        /* Construct Object */
        public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }

        /* Download File */
        public void download(string remoteFile, string localFile)
        {
            string[] tablica = null;
            tablica = directoryListDetailed(remoteFile);
           
            Regex  regex = new Regex(@"^([d-])([rwxt-]{3}){3}\s+\d{1,}\s+.*?(\d{1,})\s+(\w+\s+\d{1,2}\s+(?:\d{4})?)(\d{1,2}:\d{2})?\s+(.+?)\s?$",
 RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

            string[] tablica2 = null;
            tablica2 = regex.Split(tablica[0]);
            int filesiz = Convert.ToInt32(tablica2[3]);
            try
            {
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                /* When in doubt, use these options */
                //  ftpRequest.
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                /* Establish Return Communication with the FTP Server */
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

                /* Get the FTP Server's Response Stream */
                ftpStream = ftpResponse.GetResponseStream();
                /* Open a File Stream to Write the Downloaded File */
                FileStream localFileStream = new FileStream(localFile, FileMode.Create);
                /* Buffer for the Downloaded Data */
                byte[] byteBuffer = new byte[bufferSize];
                int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
                int down = bytesRead;
                FProgressBar.Show();
                Application.DoEvents();
                try
                {
                    while (bytesRead > 0)
                    {
                        localFileStream.Write(byteBuffer, 0, bytesRead);
                         bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                        down += bytesRead;
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
                /* Resource Cleanup */
                localFileStream.Close();
                ftpStream.Close();
                ftpResponse.Close();
                ftpRequest = null;
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
            return;
        }

public string[] directoryListDetailed(string directory)
        {
           try
            {
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                /* When in doubt, use these options */
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                /* Establish Return Communication with the FTP Server */
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                /* Establish Return Communication with the FTP Server */
                ftpStream = ftpResponse.GetResponseStream();
                /* Get the FTP Server's Response Stream */
                StreamReader ftpReader = new StreamReader(ftpStream);
                /* Store the Raw Response */
                string directoryRaw = null;
                /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
                try { while (ftpReader.Peek() != -1) { directoryRaw += ftpReader.ReadLine() + "|"; } }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
                /* Resource Cleanup */
                ftpReader.Close();
                ftpStream.Close();
                ftpResponse.Close();
                ftpRequest = null;
                /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
                try { string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            }
            catch (Exception ex) {  MessageBox.Show(ex.ToString()+"\n\n"+ex.Message.ToString()+"\n\n"+ftpRequest.ToString());
             }
            /* Return an Empty string Array if an Exception Occurs */
            return new string[] { "" };
        }
    }

Przy pierwszym wywołaniu :

Kopiuj
ftp FTPConnection = new ftp(xxx,yyy, zzz);
                          FTPConnection.download(Globalne.FTPDir + "/baza.db", Path.Combine(Directory, "baza.db"));

wszystko idzie ok, jednak przy próbie powtórzenia tego metoda directoryListDetailed wykrzacza się na

Kopiuj
     ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                /* Establish Return Communication with the FTP Server */
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                /* Establish Return Communication with the FTP Server */
                ftpStream = ftpResponse.GetResponseStream();

zwracając błąd Serwer zdalny zwrcił błąd: (500) Błąd składni, nieznane polecenie

Nie mogę zrozumieć dlaczego - możecie mi podpowiedzieć?

edytowany 1x, ostatnio: hipekk
DibbyDum
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad rok
  • Lokalizacja:Polska, Kraków
0

A próbowałeś zalogować się na serwer przy użyciu jakiegoś klienta np. PuTTY (PSFTP) i wywołać komendę LIST, może serwer nie wspiera tej komendy.

http://referencesource.microsoft.com/#System/net/System/Net/WebRequestMethods.cs,018276e93c79fd5f,references


Yubby dibby dibby dibby dibby dibby dibby dum..
edytowany 3x, ostatnio: DibbyDum
HI
  • Rejestracja:prawie 13 lat
  • Ostatnio:dzień
  • Postów:1857
0

@DibbyDum serwer na pewno obsługuje LIST .

Potwierdza to m.in. to, że błąd pojawia się dopiero przy drugim użyciu metody (przy pierwszym użyciu poprawnie działa).

N0
  • Rejestracja:ponad 17 lat
  • Ostatnio:ponad 6 lat
  • Lokalizacja:Kraków
  • Postów:767
0

A ścieżkę przy drugim wywołaniu masz na pewno taką samą jak przy pierwszym?


Pomogłem? To dobrze :)
HI
  • Rejestracja:prawie 13 lat
  • Ostatnio:dzień
  • Postów:1857
0

Tak, ścieżka na pewno się nie zmienia.

EroSanin
  • Rejestracja:prawie 11 lat
  • Ostatnio:około 7 lat
  • Postów:311
0

W jaki sposób wywołujesz tą metodę?

HI
  • Rejestracja:prawie 13 lat
  • Ostatnio:dzień
  • Postów:1857
0

Pod zdarzenie przycisku wstawione mam :

Kopiuj
ftp FTPConnection = new ftp(xxx,yyy, zzz);
FTPConnection.download(Globalne.FTPDir + "/baza.db", Path.Combine(Directory, "baza.db"));
EroSanin
  • Rejestracja:prawie 11 lat
  • Ostatnio:około 7 lat
  • Postów:311
3

Sprobuj wykorzystać:

Kopiuj
 
ftpRequest.KeepAlive = false;

alternatywnie

Kopiuj

ftpRequest.Timeout = -1;
HI
  • Rejestracja:prawie 13 lat
  • Ostatnio:dzień
  • Postów:1857
0

Wygląda na to, że po zmianie KeepAlive na false wszystko działa poprawnie :).
Dziękuję serdecznie.

Jeszcze jedna prośba: możesz mi łopatologicznie wyjaśnić co "przeszkadzało" to KeepAlive ?

EroSanin
  • Rejestracja:prawie 11 lat
  • Ostatnio:około 7 lat
  • Postów:311
0

Wygląda na to, że za każdym razem nie do końca zamykałeś połączenie, ponieważ miałeś ustawione na KeepAlive = true. Później łącząc się ponowie chciałeś utworzyć połączenie które już istniało.

Tutaj masz więcej informacji (sam możliwe że nie do końca poprawnie to rozumiem):
http://stackoverflow.com/questions/21221300/fixing-system-net-webexception-the-remote-server-returned-an-error-500-syn

HI
  • Rejestracja:prawie 13 lat
  • Ostatnio:dzień
  • Postów:1857
0

Zupełnie zmylił mnie komunikat błędu...

Jeszcze raz dziękuję :)

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.