C# 对微控制器的要求总是导致(451)局部处理错误

C# 对微控制器的要求总是导致(451)局部处理错误,c#,ftp,filezilla,fipy,pycom,C#,Ftp,Filezilla,Fipy,Pycom,我正在用C语言编写一个Windows窗体应用程序,它允许用户连接到微控制器并通过FTP上传程序文件 我可以通过Filezilla成功上传程序文件。但是,当我运行下面的代码时,我收到了错误“远程服务器返回了一个错误:(451)处理中的本地错误。”在“ftpstream.Close();”行中,“每次无论我尝试上载什么文件,无论大小或文件类型,新文件都会显示在FiPy上的Filezilla中,但大小为0字节 我已经删除了微控制器上的所有程序文件,所以我知道存储空间不是问题,我不认为这是防火墙问题,因

我正在用C语言编写一个Windows窗体应用程序,它允许用户连接到微控制器并通过FTP上传程序文件

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

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

抛出的WebException在InnerException中没有显示额外的详细信息,StatusDescription作为空字符串返回

我在论坛上发布了这个问题,是针对我正在使用的特定微控制器,但没有找到解决方案。以下是该帖子的链接,其中包括FTP日志和一些在评论中尝试过的解决方案:

我已经用尽了所有的谷歌搜索结果

有人能帮我吗

我的代码:

            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();
编辑:

来自Filezilla的FTP日志:

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中的一些底层逻辑/命令处理得更好,不管怎样,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...

尝试使用WebClient,它显然可以上传到FTP,尽管我从未使用过(它可能使用FtpWebRequest)。更值得注意的是,Msft已经放弃了FtpWebRequest,并建议使用第三方库。请参阅:我们不建议您在新开发中使用
FtpWebRequest
类。有关
FtpWebRequest
的更多信息和替代方案,请参阅GitHub上的。尽管MS已弃用
FtpWebRequest
,但它仍能正常工作。和
WebClient
在引擎盖下使用
FtpWebRequest
。因此,转向it不太可能有帮助显示来自FileZilla和的日志文件。(两人都在同一家本地电脑上运行)感谢您的回复!我尝试了WebClient,得到了同样的结果。它只是来自client.Upload()方法,我认为这不如用FtpWebRequest显示它完全来自.Close()方法那样具有描述性和帮助性。编辑了我的问题,添加了Filezilla中的日志和我的代码。“它来自client.Upload()”。。。。。这是抛出错误的那条线。我想说得更清楚些。