Output Log Using Ftpwebrequest

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

I got it to work with FluentFTP. I'm not 100% sure what the issue was, maybe using CWD fixed it, maybe some underlying logic/command in FluentFTP handled it better, either way, the FTP log now more closely matches the one from Filezilla and no more errors!

My New Code:

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

New FTP Log:

# 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...

how can I save the output of windows internal FTP client?

Not sure what's happening to you. I'd check the batch file that's running it, or maybe how you're scheduling the job.

The output is confusing as the error seems out of order, but redirecting stderr seems to work on my XP machine:

C:\Temp>ftp -s:ftpcmds.txt ftp.microsxoft.com >ftplog.txt 2>&1

C:\Temp>type ftplog.txt
ftp> Not connected.
ftp> USER sconners
Invalid command.
ftp> PASS skynet.com
Not connected.
ftp> PUT test.txt test1.txt
BYE
> ftp: connect :Unknown error number

C:\Temp>

Why FtpWebRequest is adding the user login as part of the target dataset?

Your code is correct and should work.


When I use your code, my log shows:

FtpWebRequest#7746814::.ctor(ftp://abc.wyx.state.aa.bb/'ABCD.AA.C58FC.ABC1FD.ZP3ABC')

Note the quotes. But they are missing in your log:

FtpWebRequest#53578024::.ctor(ftp://abc.wyx.state.aa.bb/ABCD.AA.C58FC.ABC1FD.ZP3ABC)

Are you really showing us your exact code and matching log file?


The WebRequest.Create creates an Uri class our of the URI string and calls .ToString() on that to log it.

And obviously the

new Uri(@"ftp://abc.wyx.state.aa.bb/'ABCD.AA.C58FC.ABC1FD.ZP3ABC'").ToString()

is:

ftp://abc.wyx.state.aa.bb/'ABCD.AA.C58FC.ABC1FD.ZP3ABC'

What does it return for you?


Note all that is happening well before it connects to the server. So there's no space for server-specific behavior.


Also, what version of .NET framework are you using? In case this behavior has changed.

Getting a error 530 Not logged in when connecting using FtpWebRequest

Your password string looks like base64-encoded.

What is actually the form used by FileZilla in its configuration file (sitemanager.xml).

So my guess is that you have copied the encoded password from the sitemanager.xml and you try to use it as a literal password in the FtpWebRequest.

Make sure you use the actual literal password. If you do not remember it, use some base64 decoder.

You will find plenty of them online.

Reusing FtpWebRequest

I don't think this will be answered so I'm "closing it" by telling you how I solved it.

Well, I didn't really solve it. I did however test the download by recreating the FtpWebRequest and noticed that on the FTP server it behaved as I wanted i.e. only one log on and then sequentially executing my requests.

This is how the code getting the file size and starting the download ended up:

// Start by fetching the file size
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(sURI);

request.Method = WebRequestMethods.Ftp.GetFileSize;
NetworkCredential nc = new NetworkCredential(sUser, sPwd);
request.Credentials = nc;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;

// Get the result (size)
FtpWebResponse resp = (FtpWebResponse)request.GetResponse();
Int64 contLen = resp.ContentLength;

// and now download the file
request = (FtpWebRequest)FtpWebRequest.Create(sURI);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = nc;
request.UseBinary = true;
request.UsePassive = true;
request.KeepAlive = true;

resp = (FtpWebResponse)request.GetResponse();

So no answer on if it's possible to reset the FtpWebRequest for re-use. But at least I know there's no redundant information being transferred.

Thanks to everybody who took an interest and spent time thinking of an answer.



Related Topics



Leave a reply



Submit