简体   繁体   English

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

[英]C# FtpWebRequest - special character “§” in password

I have got a problem using FtpWebRequest with the special character "§" in password. 使用FtpWebRequest和密码中的特殊字符“§”时遇到问题。 The code looks like this: 代码如下:

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();

The GetResponse()-Method fails with "530 User cannot log in". GetResponse()方法失败,显示“ 530用户无法登录”。 With FileZilla the login works. 使用FileZilla可以登录。

I traced the network traffic with Wireshark and noticed the following: 我使用Wireshark跟踪了网络流量,并注意到以下几点:

  • FileZilla converts the password to "pa\\302\\247", tries to log in and fails. FileZilla将密码转换为“ pa \\ 302 \\ 247”,尝试登录并失败。 After that FileZilla retries connecting with password "pa\\247" which succeeds. 之后,FileZilla尝试使用密码“ pa \\ 247”成功重试连接。
  • The FtpWebRequest converts the password to "pa\\302\\247", tries to log in and fails too and throws an Exception. FtpWebRequest将密码转换为“ pa \\ 302 \\ 247”,尝试登录并同样失败,并引发异常。

How can I force FtpWebRequest to convert the password to "pa\\247" and not to "pa\\302\\247"? 如何强制FtpWebRequest将密码转换为“ pa \\ 247”而不是“ pa \\ 302 \\ 247”?

Thank you. 谢谢。

This appears to be an encoding problem. 这似乎是编码问题。 The § character is \\302\\247 in UTF-8 and \\247 in ISO-8859-1 . §字符在UTF-8中\\ 302 \\ 247 ,在ISO-8859-1中为 \\ 247 Based on the information you provided this tells me that your request is being sent with charset UTF-8 and the server is expecting ISO-8859-1. 根据您提供的信息,这告诉我您的请求是通过字符集UTF-8发送的,并且服务器期望使用ISO-8859-1。 From that you've got two choices, if you have access to the ftp server you should be able to change the default char set. 由此,您有两种选择,如果可以访问ftp服务器,则应该可以更改默认字符集。 Otherwise you'll need to send your ftp request with the charset ISO-8859-1. 否则,您将需要使用字符集ISO-8859-1发送ftp请求。 I don't have time to test it but you can give the following code a shot (notice the added header): 我没有时间进行测试,但是您可以试一下以下代码(注意添加的标题):

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM