C#FtpWebRequest - 密码中的特殊字符“§”

时间:2013-10-07 12:36:39

标签: c# ftp passwords character ftpwebrequest

我在使用FtpWebRequest时遇到问题,密码中包含特殊字符“§”。 代码如下所示:

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ftphost"));
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.UsePassive = true;
reqFTP.EnableSsl = false;
reqFTP.Timeout = 60000;
reqFTP.UseBinary = false;
reqFTP.Credentials = new NetworkCredential("user", "pa§");

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

GetResponse() - 方法失败,“530用户无法登录”。 使用FileZilla,登录可以正常工作。

我使用Wireshark跟踪网络流量并注意到以下内容:

  • FileZilla将密码转换为“pa \ 302 \ 247”,尝试登录并失败。之后,FileZilla重试使用密码“pa \ 247”连接成功。
  • FtpWebRequest将密码转换为“pa \ 302 \ 247”,尝试登录并失败并抛出异常。

如何强制FtpWebRequest将密码转换为“pa \ 247”而不是“pa \ 302 \ 247”?

谢谢。

1 个答案:

答案 0 :(得分:0)

这似乎是编码问题。 §字符 \ 302 \ 247 UTF-8 \ 247 ISO-8859 -1 即可。根据您提供的信息,这告诉我您的请求是使用charset UTF-8发送的,服务器期望ISO-8859-1。从那里你有两个选择,如果你有权访问ftp服务器,你应该能够更改默认的字符集。否则,您需要使用charset ISO-8859-1发送ftp请求。我没有时间测试它,但你可以给出以下代码(注意添加的标题):

FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ftphost"));
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.UsePassive = true;
reqFTP.EnableSsl = false;
reqFTP.Timeout = 60000;
reqFTP.UseBinary = false;
reqFTP.Credentials = new NetworkCredential("user", "pa§");
reqFTP.Headers.Add(HttpRequestHeader.ContentType, "text/plain; charset=\"ISO-8859-1\"");

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();