private static WebOperation CreateWebOperation(string uriString, FtpMode ftpMode, string username, string password) { var webOperation = WebOperation.Create(new Uri(uriString)); webOperation.WebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); SetFtpMode((FtpWebRequest)webOperation.WebRequest, ftpMode); SetNetworkCredentials(webOperation.WebRequest, username, password); return(webOperation); }
public FtpPutCommand(string ftpSite, string user, string password, FtpMode mode, params string[] files) { _ftpSite = ftpSite; _user = user; _password = password; _files = files; _mode = mode; _command = new[] { string.Format("open {0}", _ftpSite), _user, _password, "prompt", _mode.ToString().ToLower() } .Union(_files.Select(x => string.Format("put \"{0}\"", x))).Union(new[] { "bye" }).Join(Environment.NewLine); }
public SFtpClient(string hostname, string username, string password, Protocol protocol, TimeSpan timeout, FtpMode ftpMode) { _sessionOptions = new SessionOptions { Protocol = protocol, HostName = hostname, UserName = username, Password = password, Timeout = timeout, FtpMode = ftpMode }; }
public OperateContext(string identifier , string ip, int port, FtpMode mode, string user, string pwd) { if (string.IsNullOrEmpty(identifier)) throw new ArgumentNullException("identifier"); this.m_identifier = identifier; this.m_ftpIP = ip; this.m_ftpPort = port; this.m_ftpMode = mode; this.m_userName = user; this.m_password = pwd; }
public OperateContext(string identifier , string ip, int port, FtpMode mode, string user, string pwd) { if (string.IsNullOrEmpty(identifier)) { throw new ArgumentNullException("identifier"); } this.m_identifier = identifier; this.m_ftpIP = ip; this.m_ftpPort = port; this.m_ftpMode = mode; this.m_userName = user; this.m_password = pwd; }
/// <summary> /// GetTcpClient /// </summary> /// <param name="ftpMode"></param> /// <returns></returns> private TcpClient GetTcpClient(FtpMode ftpMode) { TcpClient tcpClient = null; if (ftpMode == FtpMode.Active) { Random rnd = new Random((int)DateTime.Now.Ticks); int port = rnd.Next(DATA_PORT_RANGE_MIN, DATA_PORT_RANGE_MAX); int portHigh = port >> 8; int portLow = port & 255; IPAddress ipAddress = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0]; TcpListener tcpListener = new TcpListener(ipAddress, port); tcpListener.Start(10); SendCommand(string.Format("PORT {0},{1},{2}", ipAddress.ToString().Replace(".", ","), portHigh.ToString(CultureInfo.InvariantCulture), portLow)); AssertResult(GetResult(), 200); SendCommand("MLSD"); tcpClient = tcpListener.AcceptTcpClient(); _logger.Debug("ftp", "ftp server AcceptTcpClient"); tcpListener.Stop(); } if (ftpMode == FtpMode.Passive) { SendCommand("PASV"); Result <string> result = GetResult(); string message = result.Desception; AssertResult(result); //227 Entering Passive Mode (127,0,0,1,148,235) int startIndex = message.IndexOf('('); int endIndex = message.LastIndexOf(')'); if (startIndex == -1 || endIndex == -1) { throw new Exception(string.Format("{0} Passive Mode not implemented", message)); } startIndex++; string str = message.Substring(startIndex, endIndex - startIndex); string[] args = str.Split(','); string ip = string.Format("{0}.{1}.{2}.{3}", args[0], args[1], args[2], args[3]); int port = 256 * int.Parse(args[4]) + int.Parse(args[5]); tcpClient = new TcpClient(); tcpClient.Connect(ip, port); } return(tcpClient); }
private static void SetFtpMode(FtpWebRequest request, FtpMode ftpMode) { Debug.Assert(request != null); switch (ftpMode) { case FtpMode.Passive: request.UsePassive = true; break; case FtpMode.Active: request.UsePassive = false; break; default: throw new InvalidOperationException($"FTP Mode {ftpMode} is not valid."); } }
public static void SetFtpMode(this IFtpWebRequest request, FtpMode ftpMode) { Debug.Assert(request != null); switch (ftpMode) { case FtpMode.Passive: request.UsePassive = true; break; case FtpMode.Active: request.UsePassive = false; break; default: throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "FTP Type '{0}' is not valid.", ftpMode)); } }
/// <summary> /// Download a File via Ftp. /// </summary> /// <param name="server">Server Name or IP.</param> /// <param name="port">Server Port.</param> /// <param name="ftpPath">Path to download from on remote Ftp server.</param> /// <param name="remoteFileName">Remote file to download.</param> /// <param name="localFilePath">Path to local file.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentException">Throws if server, ftpPath, or remoteFileName is null or empty.</exception> public void FtpDownloadHelper(string server, int port, string ftpPath, string remoteFileName, string localFilePath, string username, string password, FtpMode ftpMode) { if (String.IsNullOrEmpty(server)) { throw new ArgumentException("Argument 'server' cannot be a null or empty string."); } if (String.IsNullOrEmpty(ftpPath)) { throw new ArgumentException("Argument 'ftpPath' cannot be a null or empty string."); } if (String.IsNullOrEmpty(remoteFileName)) { throw new ArgumentException("Argument 'remoteFileName' cannot be a null or empty string."); } FtpDownloadHelper(new Uri(String.Format(CultureInfo.InvariantCulture, "ftp://{0}:{1}{2}{3}", server, port, ftpPath, remoteFileName)), localFilePath, username, password, ftpMode); }
/// <summary> /// Check an FTP Connection. /// </summary> /// <param name="server">Server Name or IP.</param> /// <param name="port">Server Port.</param> /// <param name="ftpPath">Path to upload to on remote Ftp server.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <param name="callback"></param> /// <exception cref="ArgumentException">Throws if Server or FtpPath is null or empty.</exception> public IAsyncResult BeginFtpCheckConnection(string server, int port, string ftpPath, string username, string password, FtpMode ftpMode, AsyncCallback callback) { var action = new Action(() => FtpCheckConnection(server, port, ftpPath, username, password, ftpMode)); return(action.BeginInvoke(callback, action)); }
/// <summary> /// Download a File via Ftp. /// </summary> /// <param name="resourceUri">Web Resource Uri.</param> /// <param name="localFilePath">Path to local file.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentNullException">Throws if resourceUri is null.</exception> public void FtpDownloadHelper(Uri resourceUri, string localFilePath, string username, string password, FtpMode ftpMode) { if (resourceUri == null) throw new ArgumentNullException("resourceUri"); FtpDownloadHelper(WebOperation.Create(resourceUri), localFilePath, username, password, ftpMode); }
internal void FtpUploadHelper(IWebOperation ftpWebOperation, Stream localStream, string username, string password, FtpMode ftpMode) { FtpUploadHelper(ftpWebOperation, localStream, -1, username, password, ftpMode); }
internal void FtpUploadHelper(IWebOperation ftpWebOperation, Stream localStream, int maximumLength, string username, string password, FtpMode ftpMode) { if (ftpWebOperation == null) throw new ArgumentNullException("ftpWebOperation"); if (localStream == null) throw new ArgumentNullException("localStream"); ftpWebOperation.WebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); ((IFtpWebRequest)ftpWebOperation.WebRequest).SetFtpMode(ftpMode); ftpWebOperation.WebRequest.SetNetworkCredentials(username, password); if (maximumLength >= 0 && localStream.Length >= maximumLength) { localStream.Position = localStream.Length - maximumLength; } ftpWebOperation.Upload(localStream); }
public void FtpUploadHelper(string server, int port, string ftpPath, string remoteFileName, Stream localStream, string username, string password, FtpMode ftpMode) { FtpUploadHelper(server, port, ftpPath, remoteFileName, localStream, -1, username, password, ftpMode); }
/// <summary> /// Check an FTP Connection. /// </summary> /// <param name="server">Server Name or IP.</param> /// <param name="port">Server Port.</param> /// <param name="ftpPath">Path to upload to on remote Ftp server.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentException">Throws if Server or FtpPath is null or empty.</exception> public void FtpCheckConnection(string server, int port, string ftpPath, string username, string password, FtpMode ftpMode) { if (String.IsNullOrEmpty(server)) throw new ArgumentException("Argument 'server' cannot be a null or empty string."); if (String.IsNullOrEmpty(ftpPath)) throw new ArgumentException("Argument 'ftpPath' cannot be a null or empty string."); var ftpWebOperation = WebOperation.Create(new Uri(String.Format(CultureInfo.InvariantCulture, "ftp://{0}:{1}{2}", server, port, ftpPath))); FtpCheckConnection(ftpWebOperation, username, password, ftpMode); }
public static int downloadWorlds(string hostUrl, string port, string hostDirectory, string localDirectory, string userName, string password, FtpMode ftpMode) { try { // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = hostUrl, PortNumber = Int32.Parse(port), UserName = userName, Password = password, FtpMode = ftpMode }; using (Session session = new Session()) { // Will continuously report progress of synchronization session.FileTransferred += FileTransferred; // Connect session.Open(sessionOptions); //download //transfer .fwl files first since they don't trigger a backup var transferResult = session.GetFilesToDirectory(hostDirectory, localDirectory, "*.fwl"); transferResult.Check(); //transfer .db files next, but under a different file extension to delay triggering a backup transferResult = session.GetFiles($"{hostDirectory}/*.db", $@"{localDirectory}\*.dbftp"); transferResult.Check(); foreach (TransferEventArgs transfer in transferResult.Transfers) { //rename finished .db downloads to trigger a backup var dbName = transfer.Destination.Replace(".dbftp", ".db"); if (File.Exists(dbName)) { File.Delete(dbName); } var fi = new FileInfo(transfer.Destination); fi.MoveTo(fi.FullName.Replace(transfer.Destination, dbName)); } } return(0); } catch (Exception e) { _lasterror = e; System.Diagnostics.Debug.WriteLine("Error: {0}", e); return(1); } }
/// <summary> /// Get the Length of the Http Download. /// </summary> /// <param name="ftpWebOperation">Web Operation.</param> /// <param name="username">Http Login Username.</param> /// <param name="password">Http Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentNullException">Throws if resourceUri is null.</exception> public long GetFtpDownloadLength(IWebOperation ftpWebOperation, string username, string password, FtpMode ftpMode) { if (ftpWebOperation == null) throw new ArgumentNullException("ftpWebOperation", "Argument 'httpWebOperation' cannot be null."); ftpWebOperation.WebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); ((IFtpWebRequest)ftpWebOperation.WebRequest).SetFtpMode(ftpMode); ftpWebOperation.WebRequest.SetNetworkCredentials(username, password); return ftpWebOperation.GetDownloadLength(); }
/// <summary> /// Check an FTP Connection. /// </summary> /// <param name="server">Server Name or IP.</param> /// <param name="port">Server Port.</param> /// <param name="ftpPath">Path to upload to on remote Ftp server.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <param name="callback"></param> /// <exception cref="ArgumentException">Throws if Server or FtpPath is null or empty.</exception> public IAsyncResult BeginFtpCheckConnection(string server, int port, string ftpPath, string username, string password, FtpMode ftpMode, AsyncCallback callback) { var action = new Action(() => FtpCheckConnection(server, port, ftpPath, username, password, ftpMode)); return action.BeginInvoke(callback, action); }
/// <summary> /// Get the Length of the Http Download. /// </summary> /// <param name="resourceUri">Web Resource Uri.</param> /// <param name="username">Http Login Username.</param> /// <param name="password">Http Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentNullException">Throws if resourceUri is null.</exception> public long GetFtpDownloadLength(Uri resourceUri, string username, string password, FtpMode ftpMode) { if (resourceUri == null) throw new ArgumentNullException("resourceUri", "Argument 'resourceUri' cannot be null."); return GetFtpDownloadLength(WebOperation.Create(resourceUri), username, password, ftpMode); }
/// <summary> /// Get the Length of the Http Download. /// </summary> /// <param name="server">Server Name or IP.</param> /// <param name="ftpPath">Path to download from on remote Ftp server.</param> /// <param name="remoteFileName">Remote file to download.</param> /// <param name="username">Http Login Username.</param> /// <param name="password">Http Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentException">Throws if Url is null or empty.</exception> public long GetFtpDownloadLength(string server, string ftpPath, string remoteFileName, string username, string password, FtpMode ftpMode) { if (String.IsNullOrEmpty(server)) throw new ArgumentException("Argument 'server' cannot be a null or empty string."); if (String.IsNullOrEmpty(ftpPath)) throw new ArgumentException("Argument 'ftpPath' cannot be a null or empty string."); if (String.IsNullOrEmpty(remoteFileName)) throw new ArgumentException("Argument 'remoteFileName' cannot be a null or empty string."); return GetFtpDownloadLength(new Uri(String.Format(CultureInfo.InvariantCulture, "ftp://{0}{1}{2}", server, ftpPath, remoteFileName)), username, password, ftpMode); }
/// <summary> /// Download a File via Ftp. /// </summary> /// <param name="ftpWebOperation">Web Operation.</param> /// <param name="localFilePath">Path to local file.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentNullException">Throws if ftpWebOperation is null.</exception> /// <exception cref="ArgumentException">Throws if localFilePath is null or empty.</exception> public void FtpDownloadHelper(IWebOperation ftpWebOperation, string localFilePath, string username, string password, FtpMode ftpMode) { if (ftpWebOperation == null) throw new ArgumentNullException("ftpWebOperation"); if (String.IsNullOrEmpty(localFilePath)) throw new ArgumentException("Argument 'localFilePath' cannot be a null or empty string."); ftpWebOperation.WebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); ((IFtpWebRequest)ftpWebOperation.WebRequest).SetFtpMode(ftpMode); ftpWebOperation.WebRequest.SetNetworkCredentials(username, password); ftpWebOperation.Download(localFilePath); }
/// <summary> /// Check an FTP Connection. /// </summary> /// <param name="ftpWebOperation">Web Operation.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentException">Throws if ftpWebOperation is null.</exception> public void FtpCheckConnection(IWebOperation ftpWebOperation, string username, string password, FtpMode ftpMode) { if (ftpWebOperation == null) { throw new ArgumentNullException("ftpWebOperation"); } var ftpWebRequest = (IFtpWebRequest)ftpWebOperation.WebRequest; ftpWebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); ftpWebRequest.KeepAlive = false; // Close the Request ftpWebRequest.Timeout = 5000; // 5 second timeout ftpWebRequest.SetFtpMode(ftpMode); ftpWebOperation.WebRequest.SetNetworkCredentials(username, password); ftpWebOperation.CheckConnection(); }
/// <summary> /// Check an FTP Connection. /// </summary> /// <param name="ftpWebOperation">Web Operation.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentException">Throws if ftpWebOperation is null.</exception> public void FtpCheckConnection(IWebOperation ftpWebOperation, string username, string password, FtpMode ftpMode) { if (ftpWebOperation == null) throw new ArgumentNullException("ftpWebOperation"); var ftpWebRequest = (IFtpWebRequest)ftpWebOperation.WebRequest; ftpWebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); ftpWebRequest.KeepAlive = false; // Close the Request ftpWebRequest.Timeout = 5000; // 5 second timeout ftpWebRequest.SetFtpMode(ftpMode); ftpWebOperation.WebRequest.SetNetworkCredentials(username, password); ftpWebOperation.CheckConnection(); }
public static int remoteSync(string hostUrl, string port, string hostDirectory, string localDirectory, string userName, string password, FtpMode ftpMode) { try { // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = hostUrl, PortNumber = Int32.Parse(port), UserName = userName, Password = password, FtpMode = ftpMode }; using (Session session = new Session()) { // Will continuously report progress of synchronization session.FileTransferred += FileTransferred; // Connect session.Open(sessionOptions); // Synchronize files SynchronizationResult synchronizationResult; synchronizationResult = session.SynchronizeDirectories( SynchronizationMode.Local, localDirectory, hostDirectory, false); // Throw on any error synchronizationResult.Check(); } return(0); } catch (Exception e) { _lasterror = e; System.Diagnostics.Debug.WriteLine("Error: {0}", e); return(1); } }
public void FtpUploadHelper(string server, int port, string ftpPath, string localFilePath, string username, string password, FtpMode ftpMode) { FtpUploadHelper(server, port, ftpPath, localFilePath, -1, username, password, ftpMode); }
public void FtpUploadHelper(string server, int port, string ftpPath, string remoteFileName, Stream localStream, int maximumLength, string username, string password, FtpMode ftpMode) { if (String.IsNullOrEmpty(server)) throw new ArgumentException("Argument 'server' cannot be a null or empty string."); if (String.IsNullOrEmpty(ftpPath)) throw new ArgumentException("Argument 'ftpPath' cannot be a null or empty string."); if (String.IsNullOrEmpty(remoteFileName)) throw new ArgumentException("Argument 'remoteFileName' cannot be a null or empty string."); if (localStream == null) throw new ArgumentNullException("localStream"); FtpUploadHelper(WebOperation.Create(new Uri(String.Format(CultureInfo.InvariantCulture, "ftp://{0}:{1}{2}{3}", server, port, ftpPath, remoteFileName))), localStream, maximumLength, username, password, ftpMode); }
internal void FtpUploadHelper(IWebOperation ftpWebOperation, string localFilePath, string username, string password, FtpMode ftpMode) { FtpUploadHelper(ftpWebOperation, localFilePath, -1, username, password, ftpMode); }
public void FtpUploadHelper(string server, int port, string ftpPath, string remoteFileName, Stream localStream, int maximumLength, string username, string password, FtpMode ftpMode) { if (String.IsNullOrEmpty(server)) { throw new ArgumentException("Argument 'server' cannot be a null or empty string."); } if (String.IsNullOrEmpty(ftpPath)) { throw new ArgumentException("Argument 'ftpPath' cannot be a null or empty string."); } if (String.IsNullOrEmpty(remoteFileName)) { throw new ArgumentException("Argument 'remoteFileName' cannot be a null or empty string."); } if (localStream == null) { throw new ArgumentNullException("localStream"); } FtpUploadHelper(WebOperation.Create(new Uri(String.Format(CultureInfo.InvariantCulture, "ftp://{0}:{1}{2}{3}", server, port, ftpPath, remoteFileName))), localStream, maximumLength, username, password, ftpMode); }
/// <summary> /// Download a File via Ftp. /// </summary> /// <param name="ftpWebOperation">Web Operation.</param> /// <param name="localFilePath">Path to local file.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentNullException">Throws if ftpWebOperation is null.</exception> /// <exception cref="ArgumentException">Throws if localFilePath is null or empty.</exception> public void FtpDownloadHelper(IWebOperation ftpWebOperation, string localFilePath, string username, string password, FtpMode ftpMode) { if (ftpWebOperation == null) { throw new ArgumentNullException("ftpWebOperation"); } if (String.IsNullOrEmpty(localFilePath)) { throw new ArgumentException("Argument 'localFilePath' cannot be a null or empty string."); } ftpWebOperation.WebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); ((IFtpWebRequest)ftpWebOperation.WebRequest).SetFtpMode(ftpMode); ftpWebOperation.WebRequest.SetNetworkCredentials(username, password); ftpWebOperation.Download(localFilePath); }
internal void FtpUploadHelper(IWebOperation ftpWebOperation, Stream localStream, int maximumLength, string username, string password, FtpMode ftpMode) { if (ftpWebOperation == null) { throw new ArgumentNullException("ftpWebOperation"); } if (localStream == null) { throw new ArgumentNullException("localStream"); } ftpWebOperation.WebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); ((IFtpWebRequest)ftpWebOperation.WebRequest).SetFtpMode(ftpMode); ftpWebOperation.WebRequest.SetNetworkCredentials(username, password); if (maximumLength >= 0 && localStream.Length >= maximumLength) { localStream.Position = localStream.Length - maximumLength; } ftpWebOperation.Upload(localStream); }
/// <summary> /// Get the Length of the Http Download. /// </summary> /// <param name="resourceUri">Web Resource Uri.</param> /// <param name="username">Http Login Username.</param> /// <param name="password">Http Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentNullException">Throws if resourceUri is null.</exception> public long GetFtpDownloadLength(Uri resourceUri, string username, string password, FtpMode ftpMode) { if (resourceUri == null) { throw new ArgumentNullException("resourceUri", "Argument 'resourceUri' cannot be null."); } return(GetFtpDownloadLength(WebOperation.Create(resourceUri), username, password, ftpMode)); }
/// <summary> /// Download a File via Ftp. /// </summary> /// <param name="resourceUri">Web Resource Uri.</param> /// <param name="localFilePath">Path to local file.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentNullException">Throws if resourceUri is null.</exception> public void FtpDownloadHelper(Uri resourceUri, string localFilePath, string username, string password, FtpMode ftpMode) { if (resourceUri == null) { throw new ArgumentNullException("resourceUri"); } FtpDownloadHelper(WebOperation.Create(resourceUri), localFilePath, username, password, ftpMode); }
/// <summary> /// Download a File via Ftp. /// </summary> /// <param name="server">Server Name or IP.</param> /// <param name="port">Server Port.</param> /// <param name="ftpPath">Path to download from on remote Ftp server.</param> /// <param name="remoteFileName">Remote file to download.</param> /// <param name="localFilePath">Path to local file.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentException">Throws if server, ftpPath, or remoteFileName is null or empty.</exception> public void FtpDownloadHelper(string server, int port, string ftpPath, string remoteFileName, string localFilePath, string username, string password, FtpMode ftpMode) { if (String.IsNullOrEmpty(server)) throw new ArgumentException("Argument 'server' cannot be a null or empty string."); if (String.IsNullOrEmpty(ftpPath)) throw new ArgumentException("Argument 'ftpPath' cannot be a null or empty string."); if (String.IsNullOrEmpty(remoteFileName)) throw new ArgumentException("Argument 'remoteFileName' cannot be a null or empty string."); FtpDownloadHelper(new Uri(String.Format(CultureInfo.InvariantCulture, "ftp://{0}:{1}{2}{3}", server, port, ftpPath, remoteFileName)), localFilePath, username, password, ftpMode); }
/// <summary> /// Get the Length of the Http Download. /// </summary> /// <param name="server">Server Name or IP.</param> /// <param name="ftpPath">Path to download from on remote Ftp server.</param> /// <param name="remoteFileName">Remote file to download.</param> /// <param name="username">Http Login Username.</param> /// <param name="password">Http Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentException">Throws if Url is null or empty.</exception> public long GetFtpDownloadLength(string server, string ftpPath, string remoteFileName, string username, string password, FtpMode ftpMode) { if (String.IsNullOrEmpty(server)) { throw new ArgumentException("Argument 'server' cannot be a null or empty string."); } if (String.IsNullOrEmpty(ftpPath)) { throw new ArgumentException("Argument 'ftpPath' cannot be a null or empty string."); } if (String.IsNullOrEmpty(remoteFileName)) { throw new ArgumentException("Argument 'remoteFileName' cannot be a null or empty string."); } return(GetFtpDownloadLength(new Uri(String.Format(CultureInfo.InvariantCulture, "ftp://{0}{1}{2}", server, ftpPath, remoteFileName)), username, password, ftpMode)); }
public abstract void Upload(string host, int port, string ftpPath, string localFilePath, string username, string password, FtpMode ftpMode);
/// <summary> /// Get the Length of the Http Download. /// </summary> /// <param name="ftpWebOperation">Web Operation.</param> /// <param name="username">Http Login Username.</param> /// <param name="password">Http Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentNullException">Throws if resourceUri is null.</exception> public long GetFtpDownloadLength(IWebOperation ftpWebOperation, string username, string password, FtpMode ftpMode) { if (ftpWebOperation == null) { throw new ArgumentNullException("ftpWebOperation", "Argument 'httpWebOperation' cannot be null."); } ftpWebOperation.WebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); ((IFtpWebRequest)ftpWebOperation.WebRequest).SetFtpMode(ftpMode); ftpWebOperation.WebRequest.SetNetworkCredentials(username, password); return(ftpWebOperation.GetDownloadLength()); }
public abstract void Upload(string host, int port, string ftpPath, string remoteFileName, Stream stream, int maximumLength, string username, string password, FtpMode ftpMode);
/// <summary> /// Check an FTP Connection. /// </summary> /// <param name="server">Server Name or IP.</param> /// <param name="port">Server Port.</param> /// <param name="ftpPath">Path to upload to on remote Ftp server.</param> /// <param name="username">Ftp Login Username.</param> /// <param name="password">Ftp Login Password.</param> /// <param name="ftpMode">Ftp Transfer Mode.</param> /// <exception cref="ArgumentException">Throws if Server or FtpPath is null or empty.</exception> public void FtpCheckConnection(string server, int port, string ftpPath, string username, string password, FtpMode ftpMode) { if (String.IsNullOrEmpty(server)) { throw new ArgumentException("Argument 'server' cannot be a null or empty string."); } if (String.IsNullOrEmpty(ftpPath)) { throw new ArgumentException("Argument 'ftpPath' cannot be a null or empty string."); } var ftpWebOperation = WebOperation.Create(new Uri(String.Format(CultureInfo.InvariantCulture, "ftp://{0}:{1}{2}", server, port, ftpPath))); FtpCheckConnection(ftpWebOperation, username, password, ftpMode); }
public abstract void CheckConnection(string host, int port, string ftpPath, string username, string password, FtpMode ftpMode);
public override void Upload(string host, int port, string ftpPath, string remoteFileName, Stream stream, int maximumLength, string username, string password, FtpMode ftpMode) { if (host == null) { throw new ArgumentNullException(nameof(host)); } if (ftpPath == null) { throw new ArgumentNullException(nameof(ftpPath)); } if (remoteFileName == null) { throw new ArgumentNullException(nameof(remoteFileName)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } string uriString = CreateUriStringForUpload(host, port, ftpPath, remoteFileName); var webOperation = CreateWebOperation(uriString, ftpMode, username, password); if (maximumLength >= 0 && stream.Length >= maximumLength) { stream.Position = stream.Length - maximumLength; } webOperation.Upload(stream); }
public static void uploadFile(string hostUrl, string port, string hostDirectory, string localFile, string userName, string password, FtpMode ftpMode) { try { // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = hostUrl, PortNumber = Int32.Parse(port), UserName = userName, Password = password, FtpMode = ftpMode }; using (Session session = new Session()) { // Will continuously report progress of synchronization session.FileTransferred += FileTransferred; // Connect session.Open(sessionOptions); // Upload var uploadResult = session.PutFileToDirectory(localFile, hostDirectory); // Throw on any error if (uploadResult.Error != null) { throw uploadResult.Error; } } } catch (Exception e) { throw e; } }
public override void CheckConnection(string host, int port, string ftpPath, string username, string password, FtpMode ftpMode) { if (host == null) { throw new ArgumentNullException(nameof(host)); } if (ftpPath == null) { throw new ArgumentNullException(nameof(ftpPath)); } string uriString = String.Format(CultureInfo.InvariantCulture, "ftp://{0}:{1}{2}", host, port, ftpPath); var webOperation = CreateWebOperation(uriString, ftpMode, username, password); var ftpWebRequest = (FtpWebRequest)webOperation.WebRequest; ftpWebRequest.KeepAlive = false; ftpWebRequest.Timeout = 5000; webOperation.CheckConnection(); }
/// <summary> /// GetTcpClient /// </summary> /// <param name="ftpMode"></param> /// <returns></returns> private TcpClient GetTcpClient(FtpMode ftpMode) { TcpClient tcpClient = null; if(ftpMode == FtpMode.Active) { Random rnd = new Random((int)DateTime.Now.Ticks); int port = rnd.Next(DATA_PORT_RANGE_MIN, DATA_PORT_RANGE_MAX); int portHigh = port >> 8; int portLow = port & 255; IPAddress ipAddress = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0]; TcpListener tcpListener = new TcpListener(ipAddress, port); tcpListener.Start(10); SendCommand(string.Format("PORT {0},{1},{2}", ipAddress.ToString().Replace(".", ","), portHigh.ToString(CultureInfo.InvariantCulture), portLow)); AssertResult(GetResult(), 200); SendCommand("MLSD"); tcpClient = tcpListener.AcceptTcpClient(); _logger.Debug("ftp", "ftp server AcceptTcpClient"); tcpListener.Stop(); } if(ftpMode == FtpMode.Passive) { SendCommand("PASV"); Result<string> result = GetResult(); string message = result.Desception; AssertResult(result); //227 Entering Passive Mode (127,0,0,1,148,235) int startIndex = message.IndexOf('('); int endIndex = message.LastIndexOf(')'); if (startIndex == -1 || endIndex == -1) throw new Exception(string.Format("{0} Passive Mode not implemented", message)); startIndex++; string str = message.Substring(startIndex, endIndex - startIndex); string[] args = str.Split(','); string ip = string.Format("{0}.{1}.{2}.{3}", args[0], args[1], args[2],args[3]); int port = 256 * int.Parse(args[4]) + int.Parse(args[5]); tcpClient = new TcpClient(); tcpClient.Connect(ip, port); } return tcpClient; }
public override void Upload(string host, int port, string ftpPath, string localFilePath, string username, string password, FtpMode ftpMode) { if (host == null) { throw new ArgumentNullException(nameof(host)); } if (ftpPath == null) { throw new ArgumentNullException(nameof(ftpPath)); } if (localFilePath == null) { throw new ArgumentNullException(nameof(localFilePath)); } string uriString = CreateUriStringForUpload(host, port, ftpPath, localFilePath); var webOperation = CreateWebOperation(uriString, ftpMode, username, password); webOperation.Upload(localFilePath); }