-2

I have tried every variation of the below I can think of.

client.Credentials = new NetworkCredential(ftpInfo.ftpUserName, ftpInfo.ftpPassWord);
client.BaseAddress = "ftp://99.999.9.99";
var response = client.UploadFile("testFile.txt", "C:\\ftproot\\testfile\\012\\Drop\\testFile.txt");

I know the username and password are correct. If I connect to the server using filezilla from the same box it works.

I have tried not haivng ftp:// on it -- I have to be missing something very simple.

Here is the error: {"Unable to connect to the remote server"}

  • Response {System.Net.FtpWebResponse} System.Net.WebResponse {System.Net.FtpWebResponse}
  • ContentType '($exception).Response.ContentType' threw an exception of type 'System.NotImplementedException' string {System.NotImplementedException}

UPDATE: I don't know what is wrong with the question. I have given as much info as I have on it.

Here is a current test using some of the suggestions in the notes.

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("password", "loginname");
    client.UploadFile("ftp://99.999.6.130/testFile.txt", "STOR", "c:\\testfile.txt");
} 

That just states that I am not logged in.

The below is working....I will close the question out when it lets me.

Finale Update -- working solution:

public static bool UploadFile(string url, string userName, string password, string file, out string statusDescription)
{
    try
    {
        var request = (FtpWebRequest)WebRequest.Create(url);
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential(userName, password);

        // Copy the entire contents of the file to the request stream.
        var sourceStream = new StreamReader(file);
        var fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;
        var getResponse = request.GetResponse();

        Console.WriteLine($"{fileContents.Length} {getResponse} ");
    }
}
dckuehn
  • 2,427
  • 3
  • 27
  • 37
Joe Ruder
  • 2,122
  • 2
  • 23
  • 52
  • Does **downloading** a file work? – mjwills Apr 15 '19 at 12:51
  • Could be any number of things. Proxy maybe? – Liam Apr 15 '19 at 12:51
  • try using WebRequestMethods.Ftp.UploadFile as the second argument – Code Name Jack Apr 15 '19 at 12:52
  • @CodeNameJack why? `UploadFile` selects the correct method based on the URL – Panagiotis Kanavos Apr 15 '19 at 12:56
  • Do you get an error? A timeout? Is uploading too slow? If you get an error or exception, what is the *full* exception text returned by `Exception.ToString()` ? – Panagiotis Kanavos Apr 15 '19 at 12:57
  • You can use a debugging proxy like Fiddler to check what's actually being sent back and forth whether you use FileZilla or your own program. Perhaps the IP is wrong. Perhaps the credentials are wrong. – Panagiotis Kanavos Apr 15 '19 at 12:59
  • Also looks like you may need to set Accepted content type header. – Code Name Jack Apr 15 '19 at 13:01
  • 1) Did you try the basic syntax like `client.UploadFile("ftp://99.999.9.99/testFile.txt", ...)`? 2) If it does not help, post [.NET network log](https://stackoverflow.com/q/9664650/850848). 3) Can you connect to that address using any FTP client running on the same machine as your C# code? Post its log file too. – Martin Prikryl Apr 15 '19 at 13:14
  • Still waiting for the logs. – Martin Prikryl Apr 15 '19 at 14:18
  • No logs to provide Martin -- I appreciate the help and patience while I work through this. Yes, I can connect to that address using FileZilla from the same machine (as stated) and I did try a more basic syntax as you suggested. – Joe Ruder Apr 15 '19 at 14:22

1 Answers1

0

The below is a working solution.

public static bool UploadFile(string url, string userName, string password, string file, out string statusDescription)
{
    try
    {
        var request = (FtpWebRequest)WebRequest.Create(url);
        request.Method = WebRequestMethods.Ftp.UploadFile;

        request.Credentials = new NetworkCredential(userName, password);

        // Copy the entire contents of the file to the request stream.
        var sourceStream = new StreamReader(file);
        var fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;
        var getResponse = request.GetResponse();

        Console.WriteLine($"{fileContents.Length} {getResponse} ");
    }
}
Community
  • 1
  • 1
Joe Ruder
  • 2,122
  • 2
  • 23
  • 52
  • This is not an answer to the question. This is basically what `WebClient` does internally. If this works, then `WebClient` have to work too. You must have just passed wrong parameters to it. – Martin Prikryl Apr 15 '19 at 15:03
  • Thats interesting....I will go back to my original code and see what I may have missed. – Joe Ruder Apr 15 '19 at 15:18