对微控制器的 FtpWebRequest 总是导致 (451) Local error in processing

FtpWebRequest to Microcontroller always results in (451) Local error in processing

我正在用 C# 编写 Windows Forms 应用程序,允许用户连接到微控制器并通过 FTP 上传程序文件。

我可以通过Filezilla成功上传程序文件。但是当我 运行 下面的代码时,我收到错误“远程服务器返回错误:(451) 本地处理错误。”在“ftpstream.Close();”这一行每次无论我尝试上传什么文件,无论文件大小或文件类型如何,新文件都会显示在 FiPy 上的 Filezilla 中,但大小为 0 字节。

我已经删除了微控制器上的所有程序文件,所以我知道存储 space 不是问题,我不认为它是防火墙问题,因为我可以通过 Filezilla 成功上传。

抛出的 WebException 在 InnerException 中没有显示额外的细节,StatusDescription 返回为空字符串。

我post将这个问题发到了我正在使用的特定微控制器的论坛上,但没有得到解决方案。这是 link 到 post,它包括 FTP 日志和评论中尝试的一些解决方案: https://forum.pycom.io/topic/6937/fipy-ftp-upload-in-c-returns-451-local-error-in-processing

我已经用尽了所有 google 个结果。

谁能帮帮我?

我的代码:

            var deviceFilepath = "ftp://192.168.4.1/flash/main.py";
            var appFilepath = "rwis\main.py";

            string UserId = "micro";
            string Password = "python";

            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(deviceFilepath);
            ftp.Credentials = new NetworkCredential(UserId, Password);

            ftp.UsePassive = true;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;

            var buffer = File.ReadAllBytes(appFilepath);

            Stream ftpstream = ftp.GetRequestStream();
            ftpstream.Write(buffer, 0, buffer.Length);
            ftpstream.Close();

编辑:

FTP 来自 Filezilla 的日志:

2021-04-09 09:36:52 3608 1 Status: Connecting to 192.168.4.1:21...
2021-04-09 09:36:52 3608 1 Status: Connection established, waiting for welcome message...
2021-04-09 09:36:52 3608 1 Response: 220 Micropython FTP Server
2021-04-09 09:36:52 3608 1 Status: Plain FTP is insecure. Please switch to FTP over TLS.
2021-04-09 09:36:52 3608 1 Command: USER micro
2021-04-09 09:36:52 3608 1 Response: 331 
2021-04-09 09:36:52 3608 1 Command: PASS ******
2021-04-09 09:36:52 3608 1 Response: 230 
2021-04-09 09:36:52 3608 1 Status: Server does not support non-ASCII characters.
2021-04-09 09:36:52 3608 1 Status: Logged in
2021-04-09 09:36:52 3608 1 Status: Retrieving directory listing...
2021-04-09 09:36:52 3608 1 Command: PWD
2021-04-09 09:36:52 3608 1 Response: 257 /
2021-04-09 09:36:52 3608 1 Status: Directory listing of "/" successful
2021-04-09 09:36:55 3608 1 Status: Retrieving directory listing of "/flash"...
2021-04-09 09:36:55 3608 1 Command: CWD /flash
2021-04-09 09:36:55 3608 1 Response: 250 
2021-04-09 09:36:55 3608 1 Command: TYPE I
2021-04-09 09:36:55 3608 1 Response: 200 
2021-04-09 09:36:55 3608 1 Command: PASV
2021-04-09 09:36:55 3608 1 Response: 227 (192,168,4,1,7,232)
2021-04-09 09:36:55 3608 1 Command: LIST
2021-04-09 09:36:55 3608 1 Response: 150 
2021-04-09 09:36:56 3608 1 Response: 226 
2021-04-09 09:36:56 3608 1 Status: Directory listing of "/flash" successful
2021-04-09 09:36:59 3608 1 Status: Starting upload of C:\Users\fcons_tzfi6gp\Documents\GitHub\production-assembly-app\bin\Debug\rwis\rwis_config.py
2021-04-09 09:36:59 3608 1 Command: TYPE A
2021-04-09 09:36:59 3608 1 Response: 200 
2021-04-09 09:36:59 3608 1 Command: PASV
2021-04-09 09:36:59 3608 1 Response: 227 (192,168,4,1,7,232)
2021-04-09 09:36:59 3608 1 Command: STOR rwis_config.py
2021-04-09 09:36:59 3608 1 Response: 150 
2021-04-09 09:36:59 3608 1 Response: 226 
2021-04-09 09:36:59 3608 1 Status: File transfer successful, transferred 1,361 bytes in 1 second

FTP 从我的代码中登录:

System.Net Information: 0 : [15404] FtpWebRequest#47891719::.ctor(ftp://192.168.4.1/flash/rwis_config.py)
System.Net Information: 0 : [15404] FtpWebRequest#47891719::GetRequestStream(Method=STOR.)
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Created connection from 192.168.4.2:61116 to 192.168.4.1:21.
System.Net Information: 0 : [15404] Associating FtpWebRequest#47891719 with FtpControlStream#16454088
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [220 Micropython FTP Server]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [USER micro]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [331 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [PASS ********]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [230 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [502 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [PWD]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [257 /]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [TYPE I]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [200 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [PASV]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [227 (192,168,4,1,7,232)]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [STOR flash/rwis_config.py]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [150 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [451 ]

我让它与 FluentFTP 一起工作。我不是 100% 确定问题是什么,也许使用 CWD 修复了它,也许 FluentFTP 中的一些底层 logic/command 处理得更好,无论哪种方式,FTP 日志现在更紧密与 Filezilla 中的匹配,没有更多错误!

我的新密码:

        FtpClient client = new FtpClient("ftp://192.168.4.1");
        client.Credentials = new NetworkCredential(UserId, Password);
        client.DataConnectionType = FtpDataConnectionType.PASV;
        client.Connect();
        client.SetWorkingDirectory("/flash");

        client.UploadFile(appFilepath, "rwis_config.py", FtpRemoteExists.NoCheck);

        client.Disconnect();

新FTP日志:

# Connect()
Status:   Connecting to ***:21
Response: 220 Micropython FTP Server
Command:  USER ***
Response: 331 
Command:  PASS ***
Response: 230 
Command:  FEAT
Response: 211 no-features
Status:   Text encoding: System.Text.ASCIIEncoding
Command:  SYST
Response: 215 UNIX Type: L8

# SetWorkingDirectory("/flash")
Command:  CWD /flash
Response: 250 

# UploadFile("rwis\rwis_config.py", "rwis_config.py", NoCheck, False, None)

# OpenWrite("rwis_config.py", Binary)
Command:  TYPE I
Response: 200 

# OpenPassiveDataStream(PASV, "STOR rwis_config.py", 0)
Command:  PASV
Response: 227 (192,168,4,1,7,232)
Status:   Connecting to ***:2024
Command:  STOR rwis_config.py
Response: 150 
Status:   Disposing FtpSocketStream...
Response: 226 
Status:   Testing connectivity using Socket.Poll()...
Command:  QUIT
Response: 221 
Status:   Disposing FtpSocketStream...