Question

I am trying to login to a ftp server. Using the following code in C#.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp-server");

request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
// This example assumes the FTP site uses anonymous logon.
//request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
request.Credentials = new NetworkCredential("userName", "password!#£");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();

However the login fails when the password contains some special charaters. e.g ('!' or '£')? I get the following exception.

Unhandled Exception: System.Net.WebException: The remote server returned an error: (530) Not logged in.
  at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
  at System.Net.FtpWebRequest.RequestCallback(Object obj)
  at System.Net.CommandStream.Dispose(Boolean disposing)
  at System.IO.Stream.Close()
  at System.IO.Stream.Dispose()
  at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
  at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Bo
ean canReuse)
  at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
  at System.Net.FtpWebRequest.GetResponse()
  at FtpClientTest.Program.FtpWebRequest() 

I don't get any exception when the password does not contain any special characters.

No correct solution

OTHER TIPS

what ftp server is it? can you login using other programs? if yes, use wireshark to compare what goes over the wire.

So it is probably just ANSI encoding, so try

var secureString = new SecureString();
foreach (var b in Encoding.Default.GetBytes("password!#£"))
    secureString.AppendChar((char)b);

request.Credentials = new NetworkCredential("userName", secureString);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top