The remote server returned an error: 227 Entering Passive Mode (500 oops vs_utility_recv_peek: no data)
Asked Answered
F

1

11

I am having a problem connecting a Windows service to an FTP site.

I inherited a Windows service from another developer. The service connects to a 3rd party server, downloads a csv file and then processes it. For some reason, the service stopped working (well over a year ago, before I was given the project).

So I went back to basics, created a console app and tried the connection/ file download function only in that app. I have tried many different methods to connect to the FTP, but all of them return the same error to my application:

The remote server returned an error: 227 Entering Passive Mode ()

This is one of the many methods I've tried:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftpaddress/filename.csv");
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        request.Credentials = new NetworkCredential("username", "password");

        request.UsePassive = true;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

        reader.Close();
        response.Close(); 

But it falls down on this part:

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

I read in several forums that setting the UsePassive property to False fixes these errors, but all that happened to me was that I got a syntax error instead, as below:

The remote server returned an error: (500) Syntax error, command unrecognized.

The file is hosted on a 3rd party FTP server I have no control over. I can paste the URL into a browser, and I am prompted for a username and password, which then allows me through and I can download the file.

To eliminate our firewall as the cause of the problem, I ran the app on both the internal network and the WiFi (which isn't behind the firewall), and it makes no difference. I also connected through FileZilla in Default, Active and Passive modes, and it worked every time. So no problem there.

So then I ran Wireshark. Here is an image of the wire capture using Filezilla (i.e. a successful one), in Passive mode:

enter image description here

And here is the capture when connecting (and failing) using the app, with passive set to true:

enter image description here

So as you can see in the failed connection above, I can log in to the server just fine. Then for whatever reason an extra request is sent, namely "TYPE I", which prompts the response of "Switching to binary mode." The below that, I get the following:

500 oops: vsf_sysutil_recv_peek: no data

In addition, I also ran it again after setting the Passive property to false, and this is what I got that time:

enter image description here

So my question is twofold;

1, if I somehow get past the UsePassive issue and set that property to false, will that solve my problem?

2, ignoring the UsePassive property, why can't I download the file from the app, but can from everywhere else?

Faddist answered 13/6, 2014 at 16:6 Comment(4)
Yes, if you use UsePassive=false it should disappear anyay 2xx codes are not errors so I guess it's a bug in .NET code (error is something else but it reports last executed operation).Umlaut
Thanks for the response. I know the 227 is not an error code, so I assume the .NET app returns that as it was the last successful response from the server. My .net code is at the top. Does anything jump out at you as being wrong? I've tried LOTS of different methods today, but all of them do the same thing.Faddist
No, nothing seems wrong (but I'd try to add request.UseBinary = true, both with and without UsePassive). It seems a server error (or a client error because of firewall when trying to establish TCP connection?)Umlaut
Yeah, I tried it with the UseBinary property. If UseBinary is true and Use Passive is true, I get the same 227 message. If UseBinary true and UsePassive is false, I get the 500 error. Very frustrating! I don't think it's the server as I can connect with FileZilla and through the browser, and as far as the firewall I've connected both behind the firewall and completely out of it (over the guest WiFi), no change. Thanks anyway, I guess I'll have to keep going in the dark and incessantly Googling...Faddist
F
13

The issue is now resolved. It turned out to be Kaspersky's built-in firewall that was blocking the connection. It's annoying that it didn't present me with a warning when I tried to connect, but reassuring to know my PC is safe.

The clue was in the detail of the 227 return:

10051 – A socket operation was attempted to an unreachable network

Also, for anyone reaching this via Google etc, the remote server was configured to only allow Passive connections, which is why I was getting the 500 syntax error. Studying a Wire capture when downloading a file revealed that Filezilla actually reverts to Passive mode automatically if Active is selected but fails.

The code in my original post works fine now.

Faddist answered 17/6, 2014 at 10:25 Comment(2)
so what should i do with kaspersky's ?Err
I was having the same problem, but it wasn't Kaspersky, it was IIS 8.5. I had to restart the FTP service using the answer here to fix the problem: serverfault.com/questions/309964/…Amati

© 2022 - 2024 — McMap. All rights reserved.