public void FTPOps()
{
string ftpUserID = "ftpätest";
string ftpPassword = "sampleäpass";
FtpWebRequest reqFTP =
(FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://<SERVERURL>:<PORT>"));
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(ftpUserID.Normalize(), ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.UsePassive = true;
reqFTP.Proxy = null;
var response = reqFTP.GetResponse();
}
结果
(530):未登录
以下错误堆栈。
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, Boolean canReuse)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetResponse()
at ConsoleApplication1.Program.FTPOps()
at ConsoleApplication1.Program.Main(String[] args)
当通过Windows资源管理器(而非IE)访问同一FTP服务器时,相同的凭据有效。
有什么想法吗?
答案 0 :(得分:0)
IIS确实是错误的,虽然宣布支持UTF-8,但它不接受UTF-8编码的用户名。有趣的是,似乎UTF-8密码没有问题。
需要使用遗产&#34; Ansi&#34;编码用户名。
我认为你不能让FtpWebRequest
使用&#34; Ansi&#34;编码用户名。
您最好更改用户名。
如果您无法更改用户名,则必须使用能够解决IIS错误的其他FTP客户端。
例如,WinSCP .NET assembly会自动回退到&#34; Ansi&#34;使用UTF-8编码进行身份验证失败时,对凭据进行编码。为了实现这一点,默认的遗产&#34; Ansi&#34;本地计算机上的编码必须与服务器上的编码相同(在您的情况下为Windows-1252)
// Set up session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
PortNumber = port,
HostName = "ftp.example.com",
UserName = "ftpätest",
Password = "sampleäpass",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Your code
}
日志示例:
> 2017-06-22 07:54:45.067 USER ftpätest
< 2017-06-22 07:54:45.067 331 Password required
> 2017-06-22 07:54:45.067 PASS sampleäpass
< 2017-06-22 07:54:45.067 530-User cannot log in.
< 2017-06-22 07:54:45.067 Win32 error: The user name or password is incorrect.
< 2017-06-22 07:54:45.067 Error details: An error occurred during the authentication process.
< 2017-06-22 07:54:45.067 530 End
. 2017-06-22 07:54:45.128 Login data contains non-ascii characters and server might not be UTF-8 aware. Trying local charset.
> 2017-06-22 07:54:45.128 USER ftpätest
< 2017-06-22 07:54:45.129 331 Password required
> 2017-06-22 07:54:45.129 PASS sampleäpass
< 2017-06-22 07:54:45.129 230 User logged in.
(我是WinSCP的作者)