0

I have a very simple piece of code. Everything is correctly written, no typos, and i chattet with a coworker but both of us have simply no idead where the error lies in here.

It smoothly goes through undtil the GetRequestStream() where the exception pops up. It finds the files, encodes it correctly but then can't seem to connect with the server.

This is the code:

public class WebRequestUploadExample
{
    public  void WebRequestUpload()
    {


    // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://xxxxx");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential("xxxxx", "xxxxx");

        // Copy the contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader(@"D:\ftpTest\Test\Test.txt");
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

        response.Close();

    }
}
MansNotHot
  • 263
  • 1
  • 3
  • 19
  • Show us an exact exception message, its callstack and ideally a log file: https://stackoverflow.com/q/9664650/850848 – Martin Prikryl Oct 13 '17 at 12:36
  • Editing your credentials out does not help. Once you have posted anything on the Internet, you cannot take it back. You have to change your credentials. And consider the data on the FTP server as compromised. – Martin Prikryl Nov 23 '17 at 08:47

1 Answers1

1

May be your target server does not support 'passive' mode. Have you tried active-mode? request.UsePassive = false;

FtpWebRequest.UsePassive

Michael
  • 1,931
  • 2
  • 8
  • 22