George Waters

Hi everybody !!!


I have been dealing with this for a long time and I just can't find the solution. I'm using a program that returns a list of files in a directory:

Public Function GetListOfFiles(ByVal serverUri As String, _

ByVal xPath As String, _

ByVal xUsername As String, _

ByVal xPassword As String, _

ByVal xFiltrer As String) As String

Dim NewPathX As String = serverUri & xPath & xFilter

Dim retStr As String = ""

Dim responseStream As System.IO.Stream = Nothing

Dim readStream As System.IO.StreamReader = Nothing

Dim Request As FtpWebRequest

Dim Response As FtpWebResponse

Try

Request = CType(WebRequest.Create(NewPathX), FtpWebRequest)

Request.Credentials = New NetworkCredential(xUsername, xPassword)

Request.UsePassive = True

Request.Method = WebRequestMethods.Ftp.ListDirectory

Dim sp As ServicePoint = Request.ServicePoint

sp.ConnectionLimit = 2

Response = CType(Request.GetResponse, FtpWebResponse)

responseStream = Response.GetResponseStream

readStream = New System.IO.StreamReader(responseStream, System.Text.Encoding.UTF8)

If Not (readStream Is Nothing) Then

retStr = readStream.ReadToEnd()

End If

Return retStr

Catch Ex As Exception

Throw

Finally

If Not (readStream Is Nothing) Then

readStream.Close()

End If

If Not (Response Is Nothing) Then

End If

End Try

End Function

Then reads each file name to find if there are special characters,
if found, replace the characters with a: _ and rename the file so it can be downloaded with a name that FtpWebRequest can understand:

Public Function RenameFiles(ByVal xServerAndPath As String, _

ByVal xUsername As String, _

ByVal xPassword As String, _

ByVal xOldNameFile As String, _

ByVal xNewNameFile As String) As Boolean

Try

Dim ftpRequest As FtpWebRequest

Dim ftpResponse As FtpWebResponse

Dim FinalURI As Uri

Dim OldNameFile As String = System.Web.HttpUtility.UrlEncode(xOldNameFile)

FinalURI = New Uri(xServerAndPath & OldNameFile.Replace("+", "%20"))

ftpRequest = CType(WebRequest.Create(FinalURI), FtpWebRequest)

ftpRequest.Credentials = New NetworkCredential(xUsername, xPassword)

ftpRequest.Method = WebRequestMethods.Ftp.Rename

ftpRequest.RenameTo = xNewNameFile

ftpResponse = CType(ftpRequest.GetResponse, FtpWebResponse)

Return True

Catch ex As Exception

Return False

End Try

End Function



Well, sometimes I have to deal with files like EMDI_1A_NiI os_fmt.jpg, that's why I always have to check if there are special characters, replace them, and then download the file. My big problem, is that sometimes, and I mean sometimes, rename does not work, file name remains and the file obviously cannot be downloaded.

In IE7(UTF8) appears as:
EMDI_1A_Ninos_fmt.jpg

In IE7(Windows) appears as:
EMDI_1A_NiI os_fmt.jpg

In IE7(URL) appears as: EMDI_1A_Ni%CC%83os_fmt.jpg


In Windows Explorer appers as : EMDI_1A_NiI os_fmt.jpg

If I debug my application it appears as EMDI_1A_Ninos_fmt.jpg


I have done a network trace, and I have discovered that if I rename it with Explorer (Microsoft) it sends:

Request:
RNFR EMDI_1A_Nin\361os_fmt.jpg
Response:
350 File Exists, ready for destination name
Request: RNTO EMDI_1A_Ni_nos_fmt.jpg
Response:
250 RNTO command succesful

while ftpRequest trace reports:

Request:
RNFR EMDI_1A_Nin~os_fmt.jpg
Response:
550 EMDI_1A_Nin~os_fmt.jpg: No such file or directory

(Damn languajes conversions, we all should use English or Esperanto)

So what is that \361 , unicode , what should I do to make my Renamefiles function works the same way windows does , why .net framework sends "n~" instead of "n"

This is driving me crazy, please help !!!

Thanks in advance

George.





















Re: .NET Framework Networking and Communication WebRequestMethods.Ftp.Rename Problem With Special Characters

Mariya Atanasova - MSFT

Did you try using the string with the international characters directly instead of encoding it with UrlEncode(xOldNameFile) When you change from 1 encoding to another there is always a problem. I think we handle the international characters correctly in filenames... you could have potential problems if the host name contains non ASCII characters, but there should not be a problem if they are in a filename

Mariya





Re: .NET Framework Networking and Communication WebRequestMethods.Ftp.Rename Problem With Special Characters

George Waters

Yes Mariya, I have tried that and when trying to rename, I have the same result:

Request: RNFR EMDI_1A_Nin~os_fmt.jpg
Response:
550 EMDI_1A_Nin~os_fmt.jpg: No such file or directory

I think the key for this problem is to encode "n" as \361, and so all special characters, but I just can't find what system encode it is.




Re: .NET Framework Networking and Communication WebRequestMethods.Ftp.Rename Problem With Special Characters

George Waters

well, I have found that the code 361 for n is an octal codification, although I still can't find how to solve the problem

any help would be apprectiated.

thanks in advance.




Re: .NET Framework Networking and Communication WebRequestMethods.Ftp.Rename Problem With Special Characters

Mariya Atanasova - MSFT

There must be some place in your code, where you're changing the encoding yourself. The way the international characters are encoded is with HEX encoding: The correct encoding of the letter "n" is "%C3%B1" and that is exactly what we send on the wire. This is compliant with the URI RFC. Now, if the server you're hitting is rejecting that value... then your server is not RFC compliant and there is no workaround to that. There is a small chance that there is more than 1 character that looks like "n" but is encoded differently and is in a fact a different character... but you'll have to find that out for yourself.

Now, if this is the only character that you're having problems with, and you know what the filenames will be in advance, then you may send the %hh%hh value that your ftp server is expecting directly.

Here is how you can see how our encoding works. This is from the Uri class that FtpWebRequest uses internally.

using System;

using System.Net;

using System.Net.Sockets;

using System.Globalization;

namespace IRI_reproes.cs

{

class Program

{

static void Main(string[] args)

{

try

{

string uriStr = "ftp://myserver/nino.html";

Uri uri = new Uri(uriStr);

Console.WriteLine(uri.AbsoluteUri);

Console.WriteLine(uri.ToString());

}

catch (Exception ex)

{

Console.WriteLine("Unhandled exception" + ex.ToString());

}

}

}

And the output is as expected:

ftp://wit1/%C3%B1i%C3%B1o.html
ftp://wit1/nino.html

Mariya





Re: .NET Framework Networking and Communication WebRequestMethods.Ftp.Rename Problem With Special Characters

Irv

My MS Visual Basic 2005 Professional Edition says that WebRequestMethods.Ftp.Rename is used to rename DIRECTORIES, not files. This is confirmed by the following two web pages for .NET 3.0 and 2.0 respectively:

http://msdn2.microsoft.com/en-us/library/system.net.webrequestmethods.ftp.rename.aspx

http://msdn2.microsoft.com/en-us/library/system.net.webrequestmethods.ftp.rename(VS.80).aspx

So where is there any documentation that supports the idea of renaming FILES using this method





Re: .NET Framework Networking and Communication WebRequestMethods.Ftp.Rename Problem With Special Characters

Mariya Atanasova - MSFT

When you're using WebRequestMethods.Ftp.Rename this means that a "REN" command will be send to the FTP server. For compliant FTP servers this command allows you to change both the directory name / and or the file name

Mariya





Re: .NET Framework Networking and Communication WebRequestMethods.Ftp.Rename Problem With Special Characters

George Waters

You are absolutely right Mariya, it is obvious that IRV haven't played with this, and as I said above, Ftp.Rename works perfectly with files and directories, my problem is when the file has special characters... I haven't found the solution yet.





Re: .NET Framework Networking and Communication WebRequestMethods.Ftp.Rename Problem With Special Characters

Trenjeska

The problem with the default microsoft ftpserver is that it does not implement a "REN" ftp command.
_only_ "RNFR" (rename from <filename>) and "RNTO" (rename to <filename>)
So wil this ftp method still work

Trenjeska