Ver 2.0 FTP issue

  • Thread starter Thread starter Bill Musgrave
  • Start date Start date
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;
}
}
}
}
}
 
I have temporarily worked around this issue, by attempting to download the
file. If it doesn't exist the 505 error is thrown.

We have been running into a contention issue, where the process picking up
my file attempts to process before I have completed. In the past I have
worked around similar situations by ftping the file with a bogus name, and
when the file has completed writing, I then rename the file.

Unfortunately I am getting the error 500 when I attempt to rename the file!

Any help/insights would be very much appreciated.

Thanks
Bill
 
Kanchan Dhage said:
When it reaches to statement Stream writer=requestobj.GetRequestStream();
it throws an error (remote server sent an error (500) .command
unrecognised)
Plez resolve this probelm.

From
http://www.google.co.in/search?hl=e....command+unrecognised)+in+FtpWebRequest&meta=

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/


When I changed these lines to a real site with a real login, it works.

private string _username = "somename";
private string _password = "somepassword";
private string _currentDirectory = "/";
private string _hostname = "my.unix.com";
 
Back
Top