FTP over SSL issue - c#

I'm having issue uploading file to ftp site over SSL to specific directory. I'm using System.Net.FtpWebRequest class for this purpose. Upload going through fine. But the file always dropped to home directory. Any idea what might be doing wrong? Appreciate your help.
public bool UploadFile(string srcFilePath, string destFilePath = null)
{
if (String.IsNullOrWhiteSpace(srcFilePath))
throw new ArgumentNullException("Source FilePath.");
if (String.IsNullOrWhiteSpace(destFilePath))
destFilePath = Path.GetFileName(srcFilePath);
Uri serverUri = GetUri(destFilePath);
//// the serverUri should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
return false;
// get the object used to communicate with the server.
FtpWebRequest request = CreateFtpRequest(serverUri, WebRequestMethods.Ftp.UploadFile);
// read file into byte array
StreamReader sourceStream = new StreamReader(srcFilePath);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
// send bytes to server
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Debug.WriteLine("Response status: {0} - {1}", response.StatusCode, response.StatusDescription);
return true;
}
private FtpWebRequest CreateFtpRequest(Uri serverUri, string method)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.EnableSsl = true;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = true;
request.Credentials = new NetworkCredential(_userName, _password);
request.Method = method;
return request;
}
private Uri GetUri(string remoteFilePath)
{
return new Uri(_baseUri, remoteFilePath);
}

OK. Finally figured it out. It is .NET 4.0 framework issue. Build the solution with .NET 3.5, it worked beautifully.
Hate to see bugs in new releases of .NET from Microsoft and wasting lot of quality time in figuring out.

Related

FTP Upload fails 'The underlying connection was closed: An unexpected error occurred on a receive.'

I'm trying to upload files to an FTP, and have been doing so successfully until today. Here (at work) I've never had issues, but in production on-site we have had sporadic issues, but now today all of a sudden it wont work 100% of the time and I can't figure this out for the life of me.
I've tried nearly everything I could find.
This is NOT a web service.
I have increased my Timeout and ReadWriteTimeout to -1 for the FtpWebRequest.
I have set the WriteTimeout and ReadTimeout of the stream I use to write the file contents.
We have made sure outbound rules are set to allow the port we're communicating over etc and can confirm we can write/read using FileZilla from the same machine/network.
The files are NOT large. They max at 1MB and are simple text files.
Below is the method I use. Let me know if you need anymore information.
private static void FtpUpload(String username, String password, String address,
Int32 port, Boolean usePassive, String filePath)
{
try
{
String fileName = "";
fileName = Path.GetFileName(filePath);
FtpWebRequest request = null;
request = (FtpWebRequest)FtpWebRequest.Create(String.Format("ftp://{0}", address));
request.Credentials = new NetworkCredential(
username,
password);
request.UsePassive = usePassive;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = -1;
request.ReadWriteTimeout = -1;
request.KeepAlive = false;
request.Proxy = null;
Console.WriteLine(String.Format("Uploading {0}...", fileName));
Stream ftpStream = null;
using (ftpStream = request.GetRequestStream())
{
ftpStream.WriteTimeout = -1;
ftpStream.ReadTimeout = -1;
using (FileStream file = File.OpenRead(filePath))
{
file.CopyTo(ftpStream);
}
}
}
catch
{
throw;
}
}
EDIT:
This code snippit works. Note when I update this snippet to use using blocks it goes back to failing. Could the use of using block be the cause?
public static void FtpUpload(
String username,
String password,
String address,
Int32 port,
Boolean usePassive,
String filePath)
{
string ftpServerIP = String.Format("ftp://{0}", address);
string ftpUserID = username;
string ftpPassword = password;
FileInfo fileInf = new FileInfo(filePath);
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerIP);
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = false;
request.UsePassive = false;
request.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
FileStream fs = fileInf.OpenRead();
Stream strm = request.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
fs.Close();
}
catch
{
throw;
}
}
For anyone else who stumbles across this, the solution contained in
http://www.experts-exchange.com/questions/28693905/FtpWebRequest-The-underlying-connection-as-closed-An-unexpected-error-has-occurred-on-a-receive.html
worked for me, you had to set the following to false
var request = (FtpWebRequest)WebRequest.Create(finalPath);
request.UseBinary = false;
request.UsePassive = false;
This solved the problem of the FTP upload working locally and failing once deployed to UAT

System.Net.WebException: The remote server returned an error: (530) Not logged in

I have the same issue as in this question: using ftpWebRequest with an error: the remote server returned error 530 not logged in
I have tried the solutions there, but I can't get past it.
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://<ftp-ip>/uploadFTP.txt");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = false;
request.EnableSsl = true;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(<username>, <password>);
request.Timeout = -1;
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(<file to be upload>);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
Where could I possibly be going wrong ?
I removed request.timeout = -1. And, also UsePassive and EnableSsl, now it is working fine. Thanks.
I am not sure what the problem was but this is the final working code:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://<ip>/<file_name>");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential("username", "password");
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader("<file_name>");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
response.Close();

FTP request to server can not connect remote server

I found a number of similar articles here but did not manage to solve my problem still.
I am trying to upload a text file to an ftp server. I used a number of methods and all of them i get same error : "Can not connect to remote server"
Method1 :
filename is the full path where the file is located
private string Upload(string ftpServer, string userName, string password, string filename)
{
string reply = "Success";
try
{
using (System.Net.WebClient client = new System.Net.WebClient()) //System.Net.WebClient client = new System.Net.WebClient()
{
client.Credentials = new System.Net.NetworkCredential(userName, password);
client.Proxy = new WebProxy();
FileInfo fi = new FileInfo(filename);
client.UploadFile(ftpServer + "//" + fi.Name, "STOR", filename);
}
}
catch (Exception ex)
{
reply = ex.Message;
}
return reply;
}
Method2:
filename = "D:\folder\file.txt"
public static void uploadFileUsingFTP(string filename)
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + serverIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Proxy = null;
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(user, pass);
// By default KeepAlive is true, where the control connection is not closed
// after a command is executed.
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
// Notify the server about the size of the uploaded file
FileStream fs = File.OpenRead(filename);
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = Convert.ToInt32(fs.Length);
byte[] buff = new byte[buffLength];
int contentLen;
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch (Exception ex)
{
string s = ex.Message;
}
}
Method3:
public static void Sample(string filename)
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://serverip/"); //test.htm
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential (user,passs);
try
{
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(filename);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
request.Proxy = null;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
response.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
Using each of them results in the same problem and yes i am able to connect to the ftp by using filezilla and also transfer files.
I know that i must be missing something very stupid but it is taking me so much time.
Any suggestion will be appreaciated.
Connection problems can be a nuisance to sort out. A tool like WireShark can be a big help in tracking down problems, e.g. when trying active vs. passive mode FTP transfers.
I've been using the following code with good results:
bool result = false;
long length = 0;
// Set up the FTP upload.
// The URI for the request specifies the protocol, the server and the filename.
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://" + ftpServerUrl + "/" + targetFilename);
ftpRequest.EnableSsl = false;
ftpRequest.KeepAlive = true;
ftpRequest.ReadWriteTimeout = ftpTimeout; // To perform an individual read or write.
ftpRequest.Timeout = ftpTimeout; // To establish a connection or start an operation.
ftpRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
// Upload the file.
using (FileStream fileStream = File.OpenRead(filename))
{
using (Stream ftpStream = ftpRequest.GetRequestStream())
{
fileStream.CopyTo(ftpStream);
length = fileStream.Length;
ftpStream.Close();
}
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
if (ftpResponse.StatusCode == FtpStatusCode.ClosingData)
result = true;
else
throw new Exception(ftpResponse.StatusDescription + " (" + ftpResponse.StatusCode + ")");
ftpResponse.Close();
}
Have already replied in another thread, however repeating here as well. I faced the same issue, here was my solution.
request.Credentials = new NetworkCredential(
usernameVariable.Normalize(),passwordVariable.Normalize(),domainVariable.Normalize());
Details can be found here
Hope it helps someone.
Add the following in your web.config to have your FtpWebRequest use the default proxy
<defaultProxy enabled="true" useDefaultCredentials="true">
</defaultProxy>

Uploading files to FTP are corrupted once in destination

I'm creating a simple drag-file-and-upload-automatically-to-ftp windows application
and I'm using the MSDN code to upload the file to the FTP.
The code is pretty straight forward:
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(String.Format("{0}{1}", FTP_PATH, filenameToUpload));
request.Method = WebRequestMethods.Ftp.UploadFile;
// Options
request.UseBinary = true;
request.UsePassive = false;
// FTP Credentials
request.Credentials = new NetworkCredential(FTP_USR, FTP_PWD);
// Copy the contents of the file to the request stream.
StreamReader sourceStream = new StreamReader(fileToUpload.FullName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
request.ContentLength = fileContents.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
writeOutput("Upload File Complete!");
writeOutput("Status: " + response.StatusDescription);
response.Close();
and it does get uploaded to the FTP
Problem is when I see the file on a browser, or simply download and try to see it on desktop I get:
I already used request.UseBinary = false; and request.UsePassive = false; but it does not seam to do any kind of good whatsoever.
What I have found out was that, the original file has 122Kb lenght and in the FTP (and after downloading), it has 219Kb...
What am I doing wrong?
By the way, the uploadFileToFTP() method is running inside a BackgroundWorker, but I don't really thing that makes any difference...
You shouldn't use a StreamReader but only a Stream to read binary files.
Streamreader is designed to read text files only.
Try with this :
private static void up(string sourceFile, string targetFile)
{
try
{
string ftpServerIP = ConfigurationManager.AppSettings["ftpIP"];
string ftpUserID = ConfigurationManager.AppSettings["ftpUser"];
string ftpPassword = ConfigurationManager.AppSettings["ftpPass"];
////string ftpURI = "";
string filename = "ftp://" + ftpServerIP + "//" + targetFile;
FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename);
ftpReq.UseBinary = true;
ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
byte[] b = File.ReadAllBytes(sourceFile);
ftpReq.ContentLength = b.Length;
using (Stream s = ftpReq.GetRequestStream())
{
s.Write(b, 0, b.Length);
}
FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();
if (ftpResp != null)
{
MessageBox.Show(ftpResp.StatusDescription);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
The problems are caused by your code decoding the binary data to character data and back to binary data. Don't do this.
Use the UploadFile Method of the WebClient Class:
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(FTP_USR, FTP_PWD);
client.UploadFile(FTP_PATH + filenameToUpload, filenameToUpload);
}

FtpWebRequest closing the upload stream hangs on large files

I am trying to use an FtpWebRequest to upload some files. This works for smallish files (say <2MB), but when I am trying to load a 16MB file, the files uploads successfully, but when I call request.GetRequestStream().Close, the code hangs (or timesout if the timeout is low enough).
I could just a) not close it and b)not bother to get the response from the server, but that doesn't seem right! See code below (using SSL or not, the same problem occurs.)
output.Close() is the line that hangs....
public static void SendFileViaFtp(string file, string url, bool useSsl, ICredentials credentials)
{
var request = (FtpWebRequest)WebRequest.Create(url + Path.GetFileName(file));
request.EnableSsl = useSsl;
request.UseBinary = true;
request.Credentials = credentials;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = 10000000;
request.ReadWriteTimeout = 10000000;
request.KeepAlive = true;
var input = File.Open(file, FileMode.Open);
var output = request.GetRequestStream();
var buffer = new byte[1024];
var lastBytesRead = -1;
var i = 0;
while (lastBytesRead != 0)
{
i++;
lastBytesRead = input.Read(buffer, 0, 1024);
Debug.WriteLine(lastBytesRead + " " + i);
if (lastBytesRead > 0)
{
output.Write(buffer, 0, lastBytesRead);
}else
{
Debug.WriteLine("Finished");
}
}
input.Close();
output.Close();
var response = (FtpWebResponse)request.GetResponse();
response.Close();
}
Thanks,
try
// after finished uploading
request.Abort(); // <=== MAGIC PART
// befor response.Close()
var response = (FtpWebResponse)request.GetResponse();
response.Close();
taken from here
try to close output before input.
make sure the last buffer isn't to large, or you could write a few empty bytes.
I don't know if it's necessary but i always set the request contentlength to the inputfile-length.
Here is an good example: http://dotnet-snippets.de/dns/ftp-file-upload-mit-buffer-SID886.aspx

Categories

Resources