FtpWebRequest MKD fails  
Author Message
justsnafu





PostPosted: .NET Framework Networking and Communication, FtpWebRequest MKD fails Top

I'm trying to create an application that will automatically upload files to our Akamai content delivery network via FTP. One of the requirements is that if the local source directory contains a new directory that does not exist on the target FTP server I need to create it. Unfortuantely everything I've tried for this gives me back the dreaded unspecified 550 error.

Example: say I have two uris to my ftp server, the first representing a directory that DOES exist already and the second representing a directory that DOES NOT exist on the remote server.

try{
string[] uris = new string[]{"ftp://accname.upload.akamai.com/10000/existingdir", "ftp://accname.upload.akamai.com/10000/newdir"}
foreach(string uri in uris){
Uri target = new Uri(uri);
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(target);
req.Method = WebRequestMethods.Ftp.MakeDirectory;
req.Credentials = credentials;
FtpWebResponse res = (FtpWebResponse)req.GetResponse();
string desc = res.StatusDescription;
string code = res.StatusCode;
}
}
catch(Exception e){
string s = e.Message;
}

1)for the first one I'll get back the expected message: "257 download: Directory already exists."
2)the second one which 'should' create the directory throws an exception with the message: "The remote server returned an error: (550) File unavailable (e.g, file not found, no access)" In fact issuing any method on that second uri will throw the same exception.

Before you go thinking its credentials-related its not. I can log into that FTP server using command line or any client with the same credentials and issue an MKD command that works everytime.

I was e****d when I saw that they had added FTP to .NET 2.0 but now I'm just frustrated and annoyed. Please help if you can, thank you

S


.NET Development18  
 
 
justsnafu





PostPosted: .NET Framework Networking and Communication, FtpWebRequest MKD fails Top

I love it when you figure out the answer to your own questions...cept I haven't yet completely figured out the why, perhaps you can help.

If I run the exact same code but reverse the order of the uris so that the new directory create is executed first it will successfully create the directory and then fail with good ole 550 on the subsequent request to try to create the already existing directory.

It seems like this might be a threading issue or perhaps its trying to reuse the same request or something I added:

res.Close()

to the end of my method but that didnt seem to help...any gurus at msdn want to shed some light one this

thanx

S



 
 
justsnafu





PostPosted: .NET Framework Networking and Communication, FtpWebRequest MKD fails Top

doesn't look like anyone has a clue on this in this forum
Its sort of disheartening...I was pumped when I saw that .NET 2.0 had added the FtpWebRequest but it doesn't appear to be anywhere near ready for primetime. I'll leave readers with my final findings in the hopes that someone might one day fix it or find a solution:

Suspecting this might be a threading related issue I used the asynchronous model they use as an example in the MSDN documentation for FtpWebRequest: http://msdn2.microsoft.com/en-US/library/system.net.ftpwebrequest.aspx to try to create the directories. Two things I noticed:

1)as I noted earlier, if the first MKD request fails because the directory already exists then the subsequent request will always fail.
2Beyond that as far as I can tell its completely hit or miss as to whether it creates the dir or not. To test I setup only new directories hoping that if none of them already existed it would create them all successfully, heres the pattern I saw:

newdir1 - success
subdir1 - failed (550 error)
subdir2 - sucsess
subdir3 - success
newdir2 - failed (550 error - therefore all its child directories predictibly failed)
subdir1 - failed (550 error)
subdir2 - failed (550 error)
subdir3 - failed (550 error)
newdir3 - success
subdir1 - failed (550 error)
subdir2 - success
subsubdir1 - failed (550 error)
subdir2 - success

Obviously this won't work for me at all, so I'm going 3rd party library that actually works as advertized.






 
 
Mariya Atanasova - MSFT





PostPosted: .NET Framework Networking and Communication, FtpWebRequest MKD fails Top

Justin,

Before proceeding to the next FtpWebRequest you have to close the FtpResponse and the FtpWebRequest. In your code it seems you're not doing this.

You can list the parent directory, parse the response to see if the directory you want to create exists there and if it doesn't then issue a new FtpWebRequest with the Makedirectory method.

Mariya


 
 
justsnafu





PostPosted: .NET Framework Networking and Communication, FtpWebRequest MKD fails Top

If you'll seeabove in my post I do note that I added the code to close the response but it had no effect. Also, I'm not sure what you can mean when you say I need to close the FtpWebRequest object as it has no Close() method available. Here is my code:

private string createDirectory(string dirPath) {
System.Net.FtpWebRequest ftpRequest = null;
System.Net.FtpWebResponse ftpResponse = null;

try {

Uri target = new Uri(dirPath);
ftpRequest = (System.Net.FtpWebRequest)WebRequest.Create(target);
ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
ftpRequest.Credentials = credentials;
ftpResponse = (System.Net.FtpWebResponse)ftpRequest.GetResponse();
string statusDesc = ftpResponse.StatusDescription;
FtpStatusCode statusCode = ftpResponse.StatusCode;
return statusDesc;
}//try
catch (Exception exc) {
string s = exc.Message;
}//catch (Exception exc)
finally {
if(ftpResponse != null) ftpResponse.Close();
ftpRequest = null;

}

 
 
erikkl2000





PostPosted: .NET Framework Networking and Communication, FtpWebRequest MKD fails Top

Hey man did you ever figure out how to close the ftpwebrequest connection

What i am doing is take the root directory and having the FtpWebResponse send me the subdirectorys.... That works fine; UNTIL i try to get the subDirectoys of the subDirectorys and i keep getting this error telling me 505 file not found or file in use ( i know the files are their ) this has to do with a connection still open; and i can not force it closed...

Do you have any suggestions for me..

thanks alot

erik

public static List<FileDirInfo> GetFileAndDirNames(string remotePathUri)

{

if (remotePathUri.EndsWith("/"))

remotePathUri= remotePathUri.Remove(remotePathUri.Length - 1);

WebRequest ftp = BuildFtpWebRequest(new Uri(remotePathUri));

ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

//

List<FileDirInfo> result = new List<FileDirInfo>();

//

try

{//

using (WebResponse response = (WebResponse)ftp.GetResponse())

{//

using (StreamReader reader = new StreamReader(response.GetResponseStream()))

{//

string line = reader.ReadLine();

while (line != null)

{

//make the structure

result.Add(MakeReady(remotePathUri, line));

line = reader.ReadLine();

}

reader.Close();

}//

response.Close();

}//

}

catch (WebException e)

{

System.Windows.Forms.MessageBox.Show(((HttpWebResponse)e.Response).StatusCode.ToString());

}

catch (Exception e)

{

System.Windows.Forms.MessageBox.Show(e.Message);

}

finally { }

return result;

}



 
 
Mariya Atanasova - MSFT





PostPosted: .NET Framework Networking and Communication, FtpWebRequest MKD fails Top

Try setting the KeepAlive property to false

ftp.KeepAlive = false

Also, you close the response IF the request is successfull, but if you get an exception you go into the catch statement and you do not close it there.

Mariya


 
 
erikkl2000





PostPosted: .NET Framework Networking and Communication, FtpWebRequest MKD fails Top

thank you for the reply.....

I was leaving whitespaces in my uri strings after processing them from the WebRequestMethods.Ftp.ListDirectoryDetails;

thanks

erik