-1

I used the following example code to upload to an FTP, it turns out that the server don't want to use anonymous connection, but I can't figure out how to change it to be able to upload with out breaking that rule.

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

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

// Copy the contents of the file to the request stream.
byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(file))
{
   fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
}

request.ContentLength = fileContents.Length;

using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(fileContents, 0, fileContents.Length);
}

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
}
Nick3
  • 639
  • 1
  • 14
  • 40
  • What if you remove the `request.Credentials` line? -- Though to be honest, I do not really understand what you mean by *"it turns out that the server don't want to use anonymous connection, but I can't figure out how to change it to be able to upload with out breaking that rule."* -- So do you or do you not want to use anonymous connection? What exact error message do you get? – Martin Prikryl Dec 13 '19 at 09:46
  • @MartinPrikryl I dont get any error message, I just got a ban from the server for using an anonymous request. So it looks like what I'm doing is still trying to make an anonymous request. – Nick3 Dec 13 '19 at 10:21
  • So the file gets uploaded? And you get a ban only afterwards? Sounds unlikely. Show us a [log file](https://stackoverflow.com/q/9664650/850848). – Martin Prikryl Dec 13 '19 at 10:36

1 Answers1

0

Anonymous credentials are still credentials. You can get rid of them by removing the line

request.Credentials = new NetworkCredential("username", "password");

and you won't be sending any credentials to the server at all.

Nevertheless, as mentioned in "Remarks" section of the MSDN docs, FtpWebRequest class is not recommended to be used for new development. Microsoft recommends to use one of third-party libraries listed here.

I also recommend choosing one of those, as they provide more robust way to communicate with FTP servers. You can enable logging and it will be easier for you to see why exactly your communication with the server is failing. You will see all the commands your app is sending to the server and also all the responses from the server. These libraries also implement all the basic operations like listing, downloading, uploading, permission setting, sync/async etc, so you don't have to write them yourself.

Andrej Lucansky
  • 725
  • 10
  • 17
  • While it indeed can be easier to work with 3rd party library, there's no reason why `FtpWebReqest` won't work in this case. And it also [supports logging](https://stackoverflow.com/q/9664650/850848). – Martin Prikryl Dec 16 '19 at 08:36
  • Of course, you are right, I just mentioned that because it's specifically mentioned in the docs and maybe the OP is not aware of the fact that Microsoft does not recommend this approach. Would save them some time. Regarding network tracing, I am aware of that option but I believe it is not supported in .Net Core anymore. HttpClient has different trace configs now, and there is no dedicated FtpClient in the framework. – Andrej Lucansky Dec 16 '19 at 08:59