May be this question seems duplicate to you. But still I want exact cause of solution for my problem.
The problem is I am requesting a file from FTP with username and password. After ftp connection initialization, it throws me an exception WebException: Cannot open passive data connection.
But I am able to download the same file using web browser like Chrome with same username and password.
It is a Unity 3D game where user info is actually requested and some user related files will be downloaded. I am using MonoDevelop to code
The server an AWS server. Its IP address is recently changed and it was restarted. I am using the new IP and getting list of files in an XML format. I am able to parse the xml data and be able to request the file.
Here is the code sample I am using for FTP.
reqFTP = (FtpWebRequest)FtpWebRequest.CreateDefault(new Uri("ftp://" + serverIP + ":" + serverPort + "/" + this.receiveInfo.fileDirectory + "/" + downloadLoc));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
Debug.Log (reqFTP.RequestUri);
reqFTP.UseBinary = true;
reqFTP.UsePassive = true;
reqFTP.Credentials = new NetworkCredential(this.receiveInfo.ftpUsername , this.receiveInfo.ftpPassword);
object state = new object ();
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Please help me to solve this issue. Atleast some useful information is much appreciated as I am very new to this kind of stuff.
I found the answer to my question.
The problem was with the FTP configuration. When the server was restarted the old ftp settings were imposed on the server. So it was blocking all passive ftp connections from clients. The AWS support person changed the configuration, then it started working.
After the config file was changed, the issue got solved. There is no need to change the code. The code is correct.
Related
I have an application that needs to upload a file to my FTP server, but I noticed that anybody could decompile that app and get the FTP's user and password.
The code is this:
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential("ftpuser", "ftppassword");
client.UploadFile("ftp://ip/" + Computer + "_" + ExecTime + ".txt", "STOR", Application.StartupPath + #"\Stats.txt");
}
And I would like to hide those credentials from anyone. How could this be done?
It might be wiser to implement public FTP with no credentials and very limited access only to the resources you want. Obfuscation won't work since someone will de-obfuscate it anyway. Since you want to upload files from the client you may want to put some sort of controller so the upload mechanism can't be abused.
Or... simply give each user an account with limited access and problem sorted :).
I think this is more of a design problem :)
We have a Windows 2008 R2 web server with FTP over SSL. This app uses .NET 4.5 and when I upload files, the date/time on the file changes to the current date/time on the server. Is there a way to have the uploaded file preserve the original (last modified) date?
Here is what I have:
FtpWebRequest clsRequest = (FtpWebRequest)WebRequest.Create(FTPFilePath);
clsRequest.EnableSsl = true;
clsRequest.UsePassive = true;
clsRequest.Credentials = new NetworkCredential(swwwFTPUser, swwwFTPPassword);
clsRequest.Method = WebRequestMethods.Ftp.UploadFile;
Byte[] bFile = File.ReadAllBytes(LocalFilePath);
Stream clsStream = clsRequest.GetRequestStream();
clsStream.Write(bFile, 0, bFile.Length);
clsStream.Close();
clsStream.Dispose();
clsRequest = null;
There's really no standard way to update timestamp of a remote file over an FTP protocol. That's probably why the FtpWebRequest does not support it.
There are two non-standard ways to update the timestamp. Either a non-standard MFMT command:
MFMT yyyymmddhhmmss path
or a non-standard use of (otherwise standard) MDTM command:
MDTM yyyymmddhhmmss path
But the FtpWebRequest does not allow you to send a custom command either.
See for example How to send arbitrary FTP commands in C#.
So you have to use a 3rd party FTP library.
For example WinSCP .NET assembly preserves a timestamp of an uploaded file by default.
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "example.com",
UserName = "user",
Password = "mypassword",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
// Upload
session.PutFiles(#"c:\toupload\file.txt*", "/home/user/").Check();
}
See a full example.
Note that WinSCP .NET assembly is not a native .NET assembly. It's rather a thin .NET wrapper around a console application.
(I'm the author of WinSCP)
I know that we can assign file attributes:-
//Change the file created time.
File.SetCreationTime(path, dtCreation);
//Change the file modified time.
File.SetLastWriteTime(path, dtModified);
If you can extract the original date before you save it to the server, then you can change file attributes....something like this:-
Sftp sftp = new Sftp();
sftp.Connect(...);
sftp.Login(...);
// upload the file
sftp.PutFile(localFile, remoteFile);
// assign creation and modification time attributes
SftpAttributes attributes = new SftpAttributes();
System.IO.FileInfo info = new System.IO.FileInfo(localFile);
attributes.Created = info.CreationTime;
attributes.Modified = info.LastWriteTime;
// set attributes of the uploaded file
sftp.SetAttributes(remoteFile, attributes);
I hope this points you in the right direction.
This is an older question, but I will add my solution here. I used an approach similar to the solution proposed by #Martin Prikryl, using the MDTM command. His answer shows the DateTime format string as yyyymmddhhmmss which is incorrect since it does not correctly handle the month and 24 hour time format. In this answer I corrected this issue and provided a full solution using C#.
I used the FluentFTP library which handles many other aspects of working with an FTP through C# very well. To set the modified time, this library does not support it but it has an Execute method. Using the FTP command MDTM yyyyMMddHHmmss /path/to/file.txt will set the modified time of the file.
NOTE: in my instance I needed to use the universal time, which may be the case for you.
The code below shows how to connect to the FTP and set the last modified time using the Execute method and sending the MDTM command.
FtpClient client = new FtpClient("ftp-address", "username", "password");
client.Connect();
FtpReply reply = client.Execute($"MDTM {DateTime.UtcNow.ToString("yyyyMMddHHmmss")} /path/to/file.txt");
I am trying to setup a filezilla server. I have followed the instructions from here. I have write a simple c# script in order to upload data to server. My code is the following:
static void UploadFile(string filepath)
{
string m_FtpHost = "ftp://ip:port/";
string m_FtpUsername = "user";
string m_FtpPassword = "pass";
// Get an instance of WebClient
WebClient client = new System.Net.WebClient();
// parse the ftp host and file into a uri path for the upload
Uri uri = new Uri(m_FtpHost + new FileInfo(filepath).Name);
// set the username and password for the FTP server
client.Credentials = new System.Net.NetworkCredential(m_FtpUsername, m_FtpPassword);
// upload the file asynchronously, non-blocking.
client.UploadFileAsync(uri, "STOR", filepath);
}
When I am running that script from the same computer everything is working fine. When I trid to run the same script from another computer of the same lan I got issues. The file doesnt sent it properly. When I turn off the firewall however the upload is happerning normally. Any idea how to get over the firewall?
Usually the Windows asks the user to give permission to a program when it tries to use any port (the Windows get's a pop out asking to allow or disallow the program from using the port ) ...
I'm not sure how to do it but I found a link ...
I'm not sure what conditions need to be met to expose this dialog, I
would assume an application that attempts to open a listening port on
a vanilla Windows instance should always display this dialog. Why
don't you try adding your application to the 'authorized applications'
list, or opening the port manually using the Windows Firewall COM
interop (NetFwTypeLib)?
http://blogs.msdn.com/b/securitytools/archive/2009/08/21/automating-windows-firewall-settings-with-c.aspx
quoted from alexw
I am trying to moving a file from one folder to another using FtpWebRequest but i keep getting error 550. This is my code;
var requestMove = (FtpWebRequest)WebRequest.Create(Helper.PathFtp + Helper.NewFolder + file);
requestMove.Method = WebRequestMethods.Ftp.Rename;
requestMove.Credentials = networkCredential;
requestMove.RenameTo = "../" + Helper.OldFolder + file;
requestMove.GetResponse();
I can list, upload, download and delete files but moving/renaming is hopeless. I have read several posts both on stackoverflow and other sites and have tried things like setting Proxy to null and adding special characters to paths but I cant find a solution that works.
The path I use in WebRequest.Create is correct as I can delete it so it must be the RenameTo I got an issue with. Any ideas?
Error 550 means access denied. If the ftp user has sufficient rights, a program (e.g. antivirus, windows thumbnail generator etc) could have the file opened and deny your move request.
You need to contact the server administrator to get around the problem.
I've been trying to finish up a web scraper for a website of mine and have hit a stumbling block on the folder creation for image storage
My code looks like this:
//ftpUser and ftpPass are set at the head of the class
FtpWebRequest lrgRequest = (FtpWebRequest) FtpWebRequest.Create("ftp://ftp.mysite.com/httpdocs/images/large/imgFolder");
lrgRequest.Credentials = new NetworkCredential(ftpUser, ftpPass);
lrgRequest.KeepAlive = false;
lrgRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse response = (FtpWebResponse) lrgRequest.GetResponse();
Console.WriteLine(response);
When i run this code it gets to the response and throws the error 550 saying the folder isn't found
I've compared my approach to a number of examples and by the standard approach it should work. The ftp address is valid and has been checked and i'm wondering is there an issue with my server that is stopping this or is my C# causing the problem
If anyone needs any more info please just say
As always any help is greatly appreciated
Regards
Barry
Definition of FTP 550:
Requested action not taken. File unavailable (e.g., file not found, no
access).
I'd check you have the appropriate permissions (or you app does) and verfiy that the file does indeed exist.
Since you are getting a response code, I doubt your code above is the cause of the problem. However you could always check the AuthenticationLevel and ImpersonationLevel to see if these provide any useful information.