private FtpConnectionInfo ValidateFtpCredentials(string ftpAddress, int port, string userName, string password) { FtpConnectionInfo connectionInfo = new FtpConnectionInfo() { FtpAddress = ftpAddress, Port = port, UserName = userName, Password = password }; Boolean isError = true; FtpWebResponse response = null; try { response = FtpRequest(ftpAddress, port, new NetworkCredential(userName, password), "ListDirectory"); } catch (WebException ex) { connectionInfo.ErrorMessage = ex.Message; //log the exception } if (response != null && (response.StatusCode == FtpStatusCode.DataAlreadyOpen || response.StatusCode == FtpStatusCode.CommandOK || response.StatusCode == FtpStatusCode.DataAlreadyOpen || response.StatusCode == FtpStatusCode.ConnectionClosed || response.StatusCode == FtpStatusCode.ClosingData)) { isError = false; } connectionInfo.IsError = isError; return(connectionInfo); }
public string UploadFeed() { if (_smartSearchSettings.Debug && _logger != null && _logger.IsEnabled(LogLevel.Information)) { _logger.Information("Called UploadFeed()"); } string response; bool uploadSuccess = false; FtpConnectionInfo cInfo = ValidateFtpCredentials(_smartSearchSettings.FtpAddress, _smartSearchSettings.FtpPort, _smartSearchSettings.Username, _smartSearchSettings.Password); if (!cInfo.IsError) { string filePath = CommonHelper.MapPath(_smartSearchSettings.GetFullFilePath()); if (File.Exists(filePath)) { FileInfo file = new FileInfo(filePath); uploadSuccess = UploadFeed(cInfo, file); } else { cInfo.ErrorMessage = string.Format("SmartSearch Feed File Does Not Exists At The Following Location: {0}", filePath); if (_logger != null && _logger.IsEnabled(LogLevel.Information)) { _logger.Information(cInfo.ErrorMessage); } } } if (!uploadSuccess || cInfo.IsError) { response = string.Format("An Error Occured While Trying To Upload The SmartSearch Feed To The Server. ErrorDetails: {0}", cInfo.ErrorMessage); if (_logger != null && _logger.IsEnabled(LogLevel.Information)) { _logger.Information(response); } } else { response = "Upload Succeeded"; } return(response); }
private bool UploadFeed(FtpConnectionInfo connectionInfo, FileInfo file) { bool isSuccess = false; string readFileName = file.FullName; string writeFileName = file.Name; byte[] fileBuffer; using (FileStream fs = new FileStream(readFileName, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { string dat = sr.ReadToEnd(); fileBuffer = Encoding.ASCII.GetBytes(dat); } } try { var response = FtpRequest(connectionInfo.FtpAddress, connectionInfo.Port, new NetworkCredential(connectionInfo.UserName, connectionInfo.Password), "Upload", fileBuffer, writeFileName); if (response != null && (response.StatusCode == FtpStatusCode.DataAlreadyOpen || response.StatusCode == FtpStatusCode.CommandOK || response.StatusCode == FtpStatusCode.DataAlreadyOpen || response.StatusCode == FtpStatusCode.ConnectionClosed || response.StatusCode == FtpStatusCode.ClosingData)) { isSuccess = true; } } catch (WebException ex) { if (null != _logger && _logger.IsEnabled(LogLevel.Error)) { _logger.Error(ex.Message, ex); } throw; } return(isSuccess); }