The below code works when run against a server in our internal network. When I change the credentials to reflect a server outside of our network, I get a 550 error in response. When I catch the exception like so:
try {
requestStream = request.GetRequestStream();
FtpWebResponse resp = (FtpWebResponse)request.GetResponse();
}
catch(WebException e) {
string status = ((FtpWebResponse)e.Response).StatusDescription;
throw e;
}
status has a value of: "550 Command STOR failed\r\n"
I can successfully upload a file using the same credentials using a client such as Filezilla. I have already tried using SetMethodRequiresCWD() as other answers have suggested and this did not work for me.
Here is the code, which receives a list of strings that each contain a full path to a file.
private void sendFilesViaFTP(List<string> fileNames) {
FtpWebRequest request = null;
string ftpEndPoint = "ftp://pathToServer/";
string fileNameOnly; //no path
Stream requestStream;
foreach(string each in fileNames){
fileNameOnly = each.Substring(each.LastIndexOf('\\') + 1);
request = (FtpWebRequest)WebRequest.Create(ftpEndPoint + fileNameOnly);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential("username", "password");
StreamReader fileToSend = new StreamReader(each);
byte[] fileContents = Encoding.UTF8.GetBytes(fileToSend.ReadToEnd()); //this is assuming the files are UTF-8 encoded, need to confirm
fileToSend.Close();
request.ContentLength = fileContents.Length;
requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse(); //validate this in some way?
response.Close();
}
}