Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I want to do a service in C# and my service download file on a FTP

My code :
C#
public string[] directoryListSimple(ITEM Line, string directory)
        {
            try
            {
                /* Create an FTP Request */
                ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + directory);
                /* Log in to the FTP Server with the User Name and Password Provided */
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = false;
                ftpRequest.KeepAlive = true;
                /* Specify the Type of FTP Request */
                ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
                /* 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)
                {
                    Line.ERROR = "TRUE";
                    CL_TRAVAIL.Instance.LOG("ERROR 2 = " + ex.ToString());
                }
                ftpReader.Close();
                ftpStream.Close();
                ftpResponse.Close();
                ftpRequest = null;

                /* Resource Cleanup */
                /* 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());
                    Line.ERROR = "FASLE";
                    return directoryList;
                }
                catch (Exception ex)
                {
                    Line.ERROR = "TRUE";
                    CL_TRAVAIL.Instance.LOG("ERROR 2 = " + ex.ToString());
                }
                /* Return an Empty string Array if an Exception Occurs */
                return new string[] { "" };
            }
            catch (Exception ex)
            {
                Line.ERROR = "TRUE";
                CL_TRAVAIL.Instance.LOG("ERROR 3 = " + ex.ToString());
                return new string[] { "" };
            }

        }


My error is at this line : ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

<pre>
10:40:29 - ERROR 3 = System.Net.WebException: La connexion sous-jacente a été fermée : Une erreur inattendue s'est produite lors de la réception.
   à System.Net.FtpWebRequest.CheckError()
   à System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   à System.Net.CommandStream.Abort(Exception e)
   à System.Net.CommandStream.CheckContinuePipeline()
   à System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
   à System.Net.FtpDataStream.Dispose(Boolean disposing)
   à System.IO.Stream.Close()
   à System.IO.StreamReader.Dispose(Boolean disposing)
   à WindowsService.FTP.directoryListSimple(ITEM Line, String directory)



What I have tried:

Hello

I use my code in windows form application and it work, but when i use it in my service application it does not work.

Somebody have an idea

Thank you
Posted
Updated 3-May-18 4:48am
Comments
F-ES Sitecore 4-Apr-18 6:04am    
It looks like there is a problem connecting to the ftp server, it could be a network firewall issue, it could be the ftp server isn't running, it could be permissions. There is no way we can tell you why a server we have no access to can't connect to an ftp server we also have no access to. I'd start by trying to connect to the ftp server from the other server using command line tools and seeing if that at least works.
Member 2521084 4-Apr-18 6:11am    
what is the account of the service ?
Laurent mortroux 4-Apr-18 6:11am    
Thank you
yes i desactivate my firewall , but why it's work in windows form pallication and dont work in service application both on th same computeur.

But my application work with my accountand service with systèm account
Member 2521084 4-Apr-18 6:14am    
meaning "local" system account?. Try setting it up under your admin account
Laurent mortroux 4-Apr-18 6:37am    
i try with admin account i have the same probleme.

12:23:48 - ERROR 3 = System.Net.WebException: La connexion sous-jacente a été fermée : Une erreur inattendue s'est produite lors de la réception.
   à System.Net.FtpWebRequest.CheckError()
   à System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
   à System.Net.CommandStream.Abort(Exception e)
   à System.Net.CommandStream.CheckContinuePipeline()
   à System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
   à System.Net.FtpDataStream.Dispose(Boolean disposing)
   à System.IO.Stream.Close()
   à System.IO.StreamReader.Dispose(Boolean disposing)
   à WindowsService.FTP.directoryListSimple(ITEM Line, String directory)

Try setting the ftpwebrequest.timeout value to -1 (unlimited). The MS docs say it's set to that, but it's actually set to 100000 (100 seconds).
 
Share this answer
 
I use this and it's work i don't undurstand why???

public List<string> directoryListSimple(ITEM Line, string directory)
        {

            List<string> directoryList = new List<string>();

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(host + "/" + directory);
            request.Method = WebRequestMethods.Ftp.ListDirectory;

            // This example assumes the FTP site uses anonymous logon.  
            request.Credentials = new NetworkCredential(user, pass);
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            //Console.WriteLine(reader.ReadToEnd());

            while (!reader.EndOfStream)
            {
                directoryList.Add(reader.ReadLine());
            }
            Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);

            reader.Close();
            responseStream.Close();
            response.Close();
            request = null;
            reader=null;
            responseStream=null;
            response = null;
            
            return directoryList;
        }
 
Share this answer
 
the soluce working with me

try
            {

                FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(host + "/" + remoteFile);
                ftpReq.UseBinary = true;
                ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
                ftpReq.Credentials = new NetworkCredential(user, pass);

                byte[] b = File.ReadAllBytes(localFile);

                ftpReq.ContentLength = b.Length;
                using (Stream s = ftpReq.GetRequestStream())
                {
                    s.Write(b, 0, b.Length);
                }

                FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();

                if (ftpResp != null)
                {
                    OUTILS.LOG("MSG FTP= " + ftpResp.StatusDescription.ToString());
                }
            }
            catch (Exception e)
            {
                OUTILS.LOG("ERROR START");
                OUTILS.LOG("MSG = " + e.ToString());
                OUTILS.LOG("ERROR END");
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900