Trying to create directory on another machine , getting exceptions - c#

here is my code :
FtpWebRequest reqFTP = null;
Stream ftpStream = null;
string currentDir = string.Format("ftp://{0}", "10.10.10.46/E:/SERVER");
//string currentDir=string.Format("ftp://{0}","10.10.10.21/var/www/webdav/SERVER");
reqFTP = (FtpWebRequest)FtpWebRequest.Create(currentDir);
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("core", "c0relynx");
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
Whenever I execute this code I get the exception "System error".
If I use the url "10.10.10.21/var/www/webdav/SERVER" I get the exception "The remote server returned an error: (530) Not logged in".
10.10.10.46 is a windows PC and 10.10.10.21 has Ubuntu.
Can anyone tell me where I have gone wrong and what I must do to resolve this?

Your code itself is not the problem. It's not how I generally do things, but it does work for me on my test rig. The problem is most likely related to the server configuration and/or credentials.
The (530) Not logged in error is almost always because your credentials are wrong. Check that the username and password are correct for the Ubuntu server by using a standard FTP client like Filezilla or even the Windows command-line ftp client.
As for the other message, it is most likely a generic 500 response returned by the other server. There are too many possible reasons for this and not enough information.
The only thing I can suggest is again to try the operation with the same credentials in some other FTP client to see what is actually happening. Unless you're keen to use a packet trace, and know enough about the FTP protocol to understand what the packets are telling you.
At this point you're going to have to do some diagnostics on the FTP server, which is outside the scope of Stack Overflow. The superuser site is better suited for this.

Related

FtpWebRequest unable to list files/directories on server .NET Framework [duplicate]

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:
And here is the capture when connecting (and failing) using the app, with passive set to true:
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:
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?
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.

How can I force a C# FtpWebRequest to use a direct IP to get onto the Internet instead of going through an HTTP proxy?

(Related to: FTP directory listing returned as HTML instead of simple Linux ls output)
How can I force a C# program (FtpWebRequest) to use a direct IP to get onto the Internet instead of going through an HTTP proxy? (My knowledge of IT networks and related terminology is scant. Apologies in advance.)
To break out from the internal company network there is
Indirect way via the IP of a (HTTP) proxy server.
Direct IP of an internet service (not a proxy server).
I know this because, when using FileZilla with / without the proxy set in Internet Explorer, then on the remote FTP server, the logs either show the IP of the proxy, or the direct IP.
Code using C# FtpWebRequest to connect to FTP server outside of the company.
FtpWebRequest request = WebRequest.Create(uri) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = server.Credential;
request.KeepAlive = true;
request.UsePassive = true;
request.EnableSsl = false;
//proxy options
//1.
// do nothing
//2.
request.Proxy = null;
//3. setup HTTP proxy
request.Proxy = new WebProxy(proxyuri, true);
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Proxy.Credentials = new NetworkCredential("Username", "Password");
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
while (!reader.EndOfStream)
{ ... }
When I set the proxy, then the C# program (obviously) uses the proxy server to get to the FTP server. However because this is an HTTP proxy, the directory listing is returned as HTML and deleting, uploading, and making directories is not possible. The program needs to delete files, create folders on the FTP.
Similarly, when not setting the proxy, then the program uses the settings in Internet Explorer, where the proxy is set, which then again uses the HTTP proxy.
In the code, setting the proxy to null (request.Proxy = null OR WebRequest.DefaultWebProxy OR GlobalProxySelection.GetEmptyWebProxy() OR new WebProxy(); ) causes an exception "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)."
This problem only occurs for one particular external FTP server (that happens to be running vsftpd). I tested using a different external FTP and both the proxy and the non proxy / direct connection work.
Questions
1. It seems the C# program can only breakout using the proxy. Why, when the proxy is not set, the program does not use the direct IP?
2. How can I force the program to use the direct IP?
3. Could the problem be due to the FTP server (vsftpd)?
The windows hosts file (%systemroot%\system32\drivers\etc) had erroneous entries. I am not exactly sure which entry was the problem but the file now only contains one line
127.0.0.1 localhost

c# app FTP error : "227 Entering Passive Mode"

I can't figure this problem out I'm having at a client's site. The client has two sites and both run the same version of my app. At one site there are no problems but at the other I started to consistently get the following error when trying to download files from a FTP site:
"227 Entering Passive Mode (...)"
I've been reading on SO and Google and cannot figure out the problem. I want to drop this client because they are just costing money. I'm using the FTP functionality included in .NET 3.5.
Any clue as to what could be going on?? Network security changes on their end?
Test with a client ftp but only in ACTIVE MODE.
Deactivate this:
in Tools - internet options - advanced -
"Use passive FTP (for firewall and DSL modem )"
To avoid that error, just use reqFTP.UsePassive = false;
reqFTP.UsePassive = false;
Check below
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://127.0.0.1/1542");
ftpRequest.Credentials = new NetworkCredential("6584", "123456");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
ftpRequest.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
return true;
A firewall issue when dropping into a passive-mode port? Can you connect to the FTP server with Filezilla?
Your probably getting a timeout, did you check the firewall? it could be blocking your connection.

Problem with deleting a file over FTP

I am using C# to upload some file to a ftp server. If the file already existed the FtpWebRequest timed out, so I thought to deleate it first.
However the WebRequestMethods.Ftp.DeleteFile also always times out. Am I doing something wrong?
Here is my code:
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(address);
request.Credentials = new NetworkCredential(Username, Password);
request.KeepAlive = false;
request.Method = WebRequestMethods.Ftp.DeleteFile;
try
{
FtpWebResponse resp = (FtpWebResponse)request.GetResponse();
}
catch (Exception e)
{
...
}
EDIT: Oh and it doesn't matter witch file I am trying to delete. As long as the file exists the request will always time out. If the file doesn't exist a different exception is thrown.
Nothing is wrong with credentials, I can do other operations (upload/download without a problem). Also it's not a server problem, if I connect to it with a client (FileZilla) with the same username / pass everything works as it should.
Thank you for your help.
The thing I have found using this Ftp via FtpWebRequest, is it is inherently a lot slower (since it is using the HTTP protocol over port 80), and it drives me crazy because FileZilla can do it a lot quicker (obviously using FTP protocol over port 20/21). There is a open source ftp component found here, I do not know if it will work for you, but worth a shot.
I know this is a subjective answer that will get downvoted, but personally, using ftp over port 80 is bound to be a lot slower especially on file operations like what you are trying to achieve.
Do you have access to the logs of the FTP server? If you do have a look at what commands the FTPWebRequest is making. It could be that it is trying to list the directory before deleting it.
Another issue maybe that the server is in passive mode, I believe FileZilla may automagicly detect this, check the connection in filezilla to see.
Knowing what commands are sent between client and FTP server could help find out what is causing the timeout. Would it be possible to use a packet analyzer such as Ethereal to capture the communication log?
Alternative approach could be using a third party FTP component and enabling logging in it. Following code uses our Rebex FTP:
// create client
Ftp client = new Ftp();
// enable logging
client.LogWriter = new Rebex.FileLogWriter(#"c:\temp\log.txt", Rebex.LogLevel.Debug);
// connect
client.Connect("ftp.example.org");
client.Login("username", "password");
// browse directories, transfer files
client.DeleteFile("file.txt");
// disconnect
client.Disconnect();

HttpWebRequest not returning, connection closing

I have a web application that is polling a web service on another server. The server is located on the same network, and is referenced by an internal IP, running on port 8080.
Every 15 secs, a request is sent out, which receives an xml response with job information. 95% of the time, this works well, however at random times, the request to the server is null, and reports a "response forcibly closed by remote host."
Researching this issue, others have set KeepAlive = false. This has not solved the issue. The web server is running .NET 3.5 SP1.
Uri serverPath = new Uri(_Url);
// create the request and set the login credentials
_Req = (HttpWebRequest)WebRequest.Create(serverPath);
_Req.KeepAlive = false;
_Req.Credentials = new NetworkCredential(username, password);
_Req.Method = this._Method;
Call to the response:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
_ResponseStream = response.GetResponseStream();
The method for this is GET. I tried changing the timeout, but the default is large enough to take this into account.
The other request we perform is a POST to post data to the server, and we are getting the same issue randomly as well. There are no firewalls affecting this, and we ruled out the Virus scanner. Any ideas to help solving this is greatly appreciated!
Are you closing the response stream and disposing of the response itself? That's the most frequent cause of "hangs" with WebRequest - there's a limit to how many connections you can open to the same machine at the same time. The GC will finalize the connections eventually, but if you dispose them properly it's not a problem.
I wouldn't rule out network issues as a possible reason for problems. Have you run a ping to your server to see if you get dropped packets that correspond to the same times as your failed requests?
Set the timeout property of FtpWebRequest object to maximum i tried it with 4 GB File and it's working great.

Categories