Asp – Is the ftp issue a coding error or credentials error

asp.netftp

I am using ASP.NET 2.0 and I am trying, for the first time, to ftp a file through the app. There are several examples on the net. This one made the most sense to me. Being unsure of the actual local it will be going, right now, I decided to FTP it right back to my local host, figuring I have the credential so it would be a good test. However, it is failing with the following error: "Unable to connect to the remote server".

public void FTPFile()
        {
            string CompleteFTPPath = "ftp://localhost//WebSite1/test.txt";
            string CompleteLocalPath = "C:\\test_file.txt";

        //Create a FTP Request Object and Specfiy a Complete Path 
        FtpWebRequest reqObj = (FtpWebRequest)WebRequest.Create(CompleteFTPPath);

        reqObj.Method = WebRequestMethods.Ftp.UploadFile;

        reqObj.Credentials = new NetworkCredential("<my user name>", "<my pw>");

        FileStream streamObj = File.OpenRead(CompleteLocalPath);

        byte[] buffer = new byte[streamObj.Length];

        streamObj.Read(buffer, 0, buffer.Length);

        streamObj.Close();
        streamObj = null;

        reqObj.GetRequestStream().Write(buffer, 0, buffer.Length);

        reqObj = null;
    }

Best Solution

Make sure the ftp server is listening on the localhost (127.0.0.1) and not just on it's network ip address.

Related Question