c# - FtpWebRequest 到微控制器总是导致 (451) 处理中的本地错误
问题描述
我正在用 C# 编写一个 Windows 窗体应用程序,它允许用户连接到微控制器并通过 FTP 上传程序文件。
我可以通过 Filezilla 成功上传程序文件。但是当我运行下面的代码时,我收到错误“远程服务器返回错误:(451)处理中的本地错误。” 在“ftpstream.Close();”行 每次无论我尝试上传什么文件,无论大小或文件类型如何,新文件都会显示在 FiPy 上的 Filezilla 中,但大小为 0 字节。
我已经删除了微控制器上的所有程序文件,所以我知道存储空间不是问题,我不认为这是防火墙问题,因为我可以通过 Filezilla 成功上传。
抛出的 WebException 在 InnerException 中没有显示额外的详细信息,并且 StatusDescription 作为空字符串返回。
我在论坛上针对我正在使用的特定微控制器发布了这个问题,但没有得到解决方案。这是该帖子的链接,它包括 FTP 日志和在评论中尝试过的一些解决方案: https ://forum.pycom.io/topic/6937/fipy-ftp-upload-in-c-returns-451-local -处理中的错误
我已经用尽了所有谷歌搜索结果。
谁能帮我吗?
我的代码:
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...
推荐阅读
- json - 如何在 WireMock JSON 存根中使用 if 条件?
- python - 如何解释 python 测试中的覆盖率报告
- eslint - eslint 返回错误但 Github 操作仍然成功
- javascript - 轮播的下一个和上一个按钮中的错误
- opencart - 使用 CFO 扩展时,如何在 Opencart 中添加到购物车之前将产品的最终价格显示为文本?
- c++ - GLIBCXX_3.4.26 not found running cross-complied program on BeagleBone
- powershell - 在powershell中调用WMI方法时如何传递参数
- python - 为什么我的简单 python 网络爬虫运行很慢?
- sql - Oracle - 物化视图 - 如何改变
- ios - 为什么在 iOS13 中 systemFontOfSize 和 SFPro-Regular 的字体创建的文本大小不相等?