B
Bill Musgrave
I have a routine that logs on to an FTP server, and tries to get the
filesize, to determine whether the file exists or not.
I am getting inconsistent behavior between 2 internal ftp servers; one is a
windows2003, the other unix.
The attached code shows what I am doing. If I log to the windows server an
error 505 occurs when the file does not exist. When I log to the unix
machine, and call GetResponse it errors with error 500 (Syntax error,
command unrecognized) regardless whether the file exists or not.
Using 'WS_FTP Pro' I can log to either server, see the files, with their
dates and sizes just fine.
I am baffled.
Bill Musgrave
using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplicationTest{
class Program{
static void Main(){
Program p = new Program();
//p.FTPFileExists("MyTextDocument.txt"); //This exists
p.FTPFileExists("YourTextDocument.txt"); //This does NOT
}
/// <summary>
/// This information is for a Unix server
/// </summary>
private string _username = "somename";
private string _password = "somepassword";
private string _currentDirectory = "/";
private string _hostname = "my.unix.com";
/// <summary>
/// This information is for a Windows 2003 server
/// </summary>
////private string _username = "somename";
////private string _password = "somepassword";
////private string _currentDirectory = "/";
////private string _hostname = "my.winserver.com";
public bool FTPFileExists(string filename){
//Try to obtain filesize: if we get error msg containing "550"
//the file does not exist
try{
string path;
string host;
//Set path
if (filename.Contains("/")){
path = System.Convert.ToString(filename.StartsWith("/")
? String.Empty : "/") + filename; ;
}
else{
path = this._currentDirectory + filename;
}
//Set Host
if (_hostname.StartsWith("ftp://")){
host= _hostname;
}
else{
host= "ftp://" + _hostname;
}
//Create URI string
string URI = host + path;
FtpWebRequest ftp =
((FtpWebRequest)(FtpWebRequest.Create(URI)));
ftp.Credentials = new NetworkCredential(_username,
_password);
ftp.KeepAlive = false;
ftp.Method = WebRequestMethods.Ftp.GetFileSize;
//Error on next line
using (FtpWebResponse response =
((FtpWebResponse)(ftp.GetResponse()))){
//I only get to here if the file exists
response.Close();
return true;
}
}
catch (Exception ex){
if (ex is WebException){
if (ex.Message.Contains("550")){
//"The remote server returned an error: (550) File
unavailable (e.g., file not found, no access)."
//This is the expected behavior. The file doesn't
exist.
return false;
}
else{
//I am ending up here. WHY??
//"The remote server returned an error: (500) Syntax
error, command unrecognized."
throw;
}
}
else{
throw;
}
}
}
}
}
filesize, to determine whether the file exists or not.
I am getting inconsistent behavior between 2 internal ftp servers; one is a
windows2003, the other unix.
The attached code shows what I am doing. If I log to the windows server an
error 505 occurs when the file does not exist. When I log to the unix
machine, and call GetResponse it errors with error 500 (Syntax error,
command unrecognized) regardless whether the file exists or not.
Using 'WS_FTP Pro' I can log to either server, see the files, with their
dates and sizes just fine.
I am baffled.
Bill Musgrave
using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplicationTest{
class Program{
static void Main(){
Program p = new Program();
//p.FTPFileExists("MyTextDocument.txt"); //This exists
p.FTPFileExists("YourTextDocument.txt"); //This does NOT
}
/// <summary>
/// This information is for a Unix server
/// </summary>
private string _username = "somename";
private string _password = "somepassword";
private string _currentDirectory = "/";
private string _hostname = "my.unix.com";
/// <summary>
/// This information is for a Windows 2003 server
/// </summary>
////private string _username = "somename";
////private string _password = "somepassword";
////private string _currentDirectory = "/";
////private string _hostname = "my.winserver.com";
public bool FTPFileExists(string filename){
//Try to obtain filesize: if we get error msg containing "550"
//the file does not exist
try{
string path;
string host;
//Set path
if (filename.Contains("/")){
path = System.Convert.ToString(filename.StartsWith("/")
? String.Empty : "/") + filename; ;
}
else{
path = this._currentDirectory + filename;
}
//Set Host
if (_hostname.StartsWith("ftp://")){
host= _hostname;
}
else{
host= "ftp://" + _hostname;
}
//Create URI string
string URI = host + path;
FtpWebRequest ftp =
((FtpWebRequest)(FtpWebRequest.Create(URI)));
ftp.Credentials = new NetworkCredential(_username,
_password);
ftp.KeepAlive = false;
ftp.Method = WebRequestMethods.Ftp.GetFileSize;
//Error on next line
using (FtpWebResponse response =
((FtpWebResponse)(ftp.GetResponse()))){
//I only get to here if the file exists
response.Close();
return true;
}
}
catch (Exception ex){
if (ex is WebException){
if (ex.Message.Contains("550")){
//"The remote server returned an error: (550) File
unavailable (e.g., file not found, no access)."
//This is the expected behavior. The file doesn't
exist.
return false;
}
else{
//I am ending up here. WHY??
//"The remote server returned an error: (500) Syntax
error, command unrecognized."
throw;
}
}
else{
throw;
}
}
}
}
}