The underlying connection was closed The server committed a protocol violation c# - c#

While executing the below code I am getting the following error
(The underlying connection was closed The server committed a protocol
violation )
. Please help on this. I almost go throw every article about the error still not getting the solution.
Thanks in Advance.
private static void Upload()
{
string sourceFile = System.IO.Path.Combine(filepath, filename);
string ftpServerIP = "Hostname";
string ftpUserID = "UserName";
string ftpPassword = "Password";
FileInfo fileInf = new FileInfo(sourceFile);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// 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
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
//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);
// Until 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)
//{
// Console.Write(ex.Message, "Upload Error");
//}
}

I found the answer for the problem. I was using SFTP server instead of FTP the code was entirely different.
So I changed the code.
using Renci.SshNet;
public static void UploadSFTPFile()
{
_SftpDetails = ReadIniFile(IniFilePath, "Sftp_Settings", SftpDetails);
string sourcefile = System.IO.Path.Combine(filepath, filename);
string destinationpath = _SftpDetails["SftpDestinationFolder"];
string host = _SftpDetails["Host"];
string username = _SftpDetails["UserName"];
string password = _SftpDetails["Password"];
int port = Convert.ToInt32(_SftpDetails["Port"]); //Port 22 is defaulted for SFTP upload
try
{
using (SftpClient client = new SftpClient(host, port, username, password))
{
client.Connect();
client.ChangeDirectory(destinationpath);
using (FileStream fs = new FileStream(sourcefile, FileMode.Open))
{
client.BufferSize = 4 * 1024;
client.UploadFile(fs, Path.GetFileName(sourcefile));
}
}
}
catch(Exception ex)
{
Console.Write(ex.Message);
return;
}
}
you have to install .sshNet from the NuGet Package Manager

Related

Sending multiple files over a single FTP connection

I am trying to modify this FTP connection method to upload multiple files in a single connection.
As you can see I have got a while loop that iterates through a filename and filepath array, I have set the WebRequest to keep alive. I am not sure what I should move out of the loop to stop new connections from constantly opening up.
This is the error I am getting:
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
Thanks in advance!
Public string FTPUploadMultipleFiles(string ftpURL, string Username, string Password, string[] filePaths, string[] fileNames)
{
string result = "OK";
try
{
int Counter = 0;
if (filePaths.Count() == fileNames.Count())
while (Counter <= filePaths.Count() - 1)
{
FileInfo fileInf = new FileInfo(filePaths[Counter]);
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURL + "/" + fileNames[Counter]));
reqFTP.Credentials = new NetworkCredential(Username, Password);
reqFTP.KeepAlive = true;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UsePassive = true;
reqFTP.UseBinary = true;
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 204800;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
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, Convert.ToInt32(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();
Counter++;
}
catch (Exception ex)
{
result = ex.Message;
}
}
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}

I get a timeout error when using FtpWebRequest with SSL

I have to upload some files on a FTP who use TLS with my C# application ( .net 3.5 )
With Filezila, no problems.
Now, with my C# code, i have a timeout exception and i really don't know why because all is ok with Filezila.
Here is my code :
public static bool AcceptAllCertificatePolicy(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
public static string Upload_SSL(string filenameSrc)
{
ServicePointManager.ServerCertificateValidationCallback = AcceptAllCertificatePolicy;
FileInfo fileInfSrc = new FileInfo(filenameSrc);
FtpWebRequest reqFTP;
// Create FtpWebRequest object from the Uri provided
if (strDirectory.Trim() != "")
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + strDirectory.Trim() + "/" + fileInfSrc.Name.Trim()));
}
else
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strHost.Trim() + "/" + fileInfSrc.Name.Trim()));
}
reqFTP.EnableSsl = true;
// Provide the WebPermission Credintials
reqFTP.Credentials = new NetworkCredential(strUser.Trim(), strPass.Trim());
reqFTP.Proxy = null;
// 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
reqFTP.ContentLength = fileInfSrc.Length;
// The buffer size is set to 8kb
int buffLength = 8192;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInfSrc.OpenRead();
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)
{
fs.Close();
return (ex.Message);
}
return "ok";
}
Now, the call :
myFtp.Class1.strHost = "ftp://XXXXXXXXXXXXX";
myFtp.Class1.strPass = "*****************";
myFtp.Class1.strUser = "*********";
myFtp.Class1.nPort = 21;
myFtp.Class1.Upload_SSL(#"D:\Test.txt");
As information, if i try to use my code in order to upload a file on a NON TLS/SLL server, all is ok. So the problem appear to be only for the TLS/SSL ftp.
Anyone have some ideas please ?
Thanks a lot,
I kept the FTP:// in the host, so the url was FTP://FTP:/xxxxx

Invalid URI: The format of the URI could not be determined using file upload to ftp [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have tried this code for uploading files to ftp server but this error comes up.I dont where am I going wrong.
I tried various ways changing my ftpurl format but still no luck.
Code:
private void button1_Click_1(object sender, EventArgs e)
{
UploadFileToFTP(sourcefilepath);
}
private static void UploadFileToFTP(string source)
{
String sourcefilepath = "C:/Users/Desktop/LUVS/*.xml";
String ftpurl = "100.100.0.35"; // e.g. fake IDs
String ftpusername = "ftp"; // e.g. fake username
String ftppassword = "1now"; // e.g. fake password
try
{
string filename = Path.GetFileName(source);
string ftpfullpath = ftpurl;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(source);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (Exception ex)
{
throw ex;
}
The error is in ftp url. You have not included the file name. Write it like this:
private static void UploadFileToFTP(string source)
{
String sourcefilepath = "C:\\Users\\Desktop\\LUVS\\a.xml";
String ftpurl = "100.100.0.35"; // e.g. fake IDs
String ftpusername = "ftp"; // e.g. fake username
String ftppassword = "1now"; // e.g. fake password
try
{
string filename = Path.GetFileName(sourcefilepath);
string ftpfullpath = "ftp://" + ftpurl + "/" + filename ;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(sourcefilepath); // here, use sourcefilepath insted of source.
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch (Exception ex)
{
throw ex;
}
}
The Create method appears to be on WebRequest, not on FtpWebRequest. And WebRequest needs to determine from the URI which child object to create (in this case FtpWebRequest) based on the format of the URI. But your URI is just a bare address:
"100.100.0.35"
If you prepend a protocol, it should be able to determine what it needs from the URI:
"ftp://100.100.0.35"

upload a XML file using FTP in c#

Please tell me how to upload a XML file using FTP in c#? Im currently using FtpWebRequest method and its giving me errors
my code is
//Create FTP request
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://www.itsthe1.com/profiles/nuwan/sample.txt");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(Username, Password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
//Load the file
FileStream stream = File.OpenRead(#"C:\sample.txt");
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Close();
//Upload file
Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
Here is my successfully working code to upload any file through FTP (I've written it in VB.NET but hopefully you can convert it to C# using any of the online translators)
Public Function Upload(ByVal fi As FileInfo, Optional ByVal targetFilename As String = "") As Boolean
'copy the file specified to target file: target file can be full path or just filename (uses current dir)
'1. check target
Dim target As String
If targetFilename.Trim = "" Then
'Blank target: use source filename & current dir
target = GetCurrentUrl() & "/" & fi.Name
Else
'otherwise treat as filename only, use current directory
target = GetCurrentUrl() & "/" & targetFilename
End If
Dim URI As String = target 'GetCurrentUrl() & "/" & target
'perform copy
Dim ftp As Net.FtpWebRequest = GetRequest(URI)
'Set request to upload a file in binary
ftp.Method = Net.WebRequestMethods.Ftp.UploadFile
ftp.UseBinary = True
'Notify FTP of the expected size
ftp.ContentLength = fi.Length
'create byte array to store: ensure at least 1 byte!
Const BufferSize As Integer = 2048
Dim content(BufferSize - 1) As Byte, dataRead As Integer
'open file for reading
Using fs As FileStream = fi.OpenRead()
Try
'open request to send
Using rs As Stream = ftp.GetRequestStream
Dim totBytes As Long = 0
Do
dataRead = fs.Read(content, 0, BufferSize)
rs.Write(content, 0, dataRead)
totBytes += dataRead
RaiseEvent StatusChanged(totBytes.ToString() & " bytes sent...")
Loop Until dataRead < BufferSize
rs.Close()
RaiseEvent StatusChanged("File uploaded successfully")
End Using
Catch ex As Exception
RaiseEvent StatusChanged("Error: " & ex.Message)
Finally
'ensure file closed
fs.Close()
End Try
End Using
ftp = Nothing
Return True
End Function
Here is the link to the entire article about developing this FTP Client:
http://dot-net-talk.blogspot.com/2008/12/how-to-create-ftp-client-in-vbnet.html
protected void Button1_Click(object sender, EventArgs e)
{
string server = "-------"; //your ip address
string ftpPath = "ftp://" + server + "//Files//" +FileUpload1.FileName;
string uname = "-----";
string password = "----------";
string filePath = Server.MapPath("~") + "\\" + FileUpload1.FileName;
try
{
UploadToFTP(ftpPath,filePath,uname,password);
}
catch (Exception ex)
{
//logic here
}
}
`private bool UploadToFTP(string strFTPFilePath, string strLocalFilePath, string strUserName, string strPassword)
{
try
{
//Create a FTP Request Object and Specfiy a Complete Path
// System.Net.WebRequest.Create(strFTPFilePath);
FtpWebRequest reqObj = (FtpWebRequest)WebRequest.Create(strFTPFilePath);
//Call A FileUpload Method of FTP Request Object
reqObj.Method = WebRequestMethods.Ftp.UploadFile;
//If you want to access Resourse Protected,give UserName and PWD
reqObj.Credentials = new NetworkCredential(strUserName, strPassword);
// Copy the contents of the file to the byte array.
byte[] fileContents = File.ReadAllBytes(strLocalFilePath);
reqObj.ContentLength = fileContents.Length;
//Upload File to FTPServer
Stream requestStream = reqObj.GetRequestStream();
// Stream requestStream = response.GetResponseStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)reqObj.GetResponse();
response.Close();
Label1.Text = "File Transfered Completed" + response.StatusDescription;
}
catch (Exception Ex)
{
throw Ex;
}
return true;
} `

Downloading ZIP file from FTP and copying to folder within website

Finding some problems copying a zip file from an FTP location. It is just copying and empty file so I think there is something wrong with my use of StreamReader or StreamWriter.
Here is the code:
//read through directory details response
string line = reader.ReadLine();
while (line != null)
{
if (line.EndsWith("zip")) //"d" = dir don't need "." or ".." dirs
{
FtpWebRequest downloadRequest = (FtpWebRequest)FtpWebRequest.Create("ftp://" + ftpHost + line); //new Uri("ftp://" + ftpServerIP + DestinationFolder + fileInf.Name));
downloadRequest.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["FilesUser"], ConfigurationManager.AppSettings["FilesPass"]);
downloadRequest.KeepAlive = false;
downloadRequest.UseBinary = true;
downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
string folderToWrite = HttpContext.Current.Server.MapPath("~/Routing/RoutingFiles/");
string folderToSave = HttpContext.Current.Server.MapPath("~/Routing/");
StreamReader downloadRequestReader = new StreamReader(downloadRequest.GetResponse().GetResponseStream());
DirectoryInfo downloadDirectory = new DirectoryInfo(folderToWrite);
FileInfo file = new FileInfo(Path.Combine(downloadDirectory.FullName, line));
if (!file.Exists)
{
StreamWriter writer = new StreamWriter(Path.Combine(folderToWrite, line), false);
writer.Write(downloadRequestReader.ReadToEnd());
using (var downloadResponseStream = response.GetResponseStream())
{
}
}
}
}
By the time it gets to the bottom of that section, the file has been copied but is empty so I don't think I'm reading the stream correctly for a zip file. Anyone any ideas? I've seen talk of FileStream being better for downloading Zip files, but I couldn't get that to work either.
Thanks.
Here is an example that downloads a file from an ftp.
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpAddr + "test.zip");
request.Credentials = new NetworkCredential(userName, password);
request.UseBinary = true; // Use binary to ensure correct dlv!
request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writer = new FileStream("test.zip", FileMode.Create);
long length = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[2048];
readCount = responseStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
writer.Write(buffer, 0, readCount);
readCount = responseStream.Read(buffer, 0, bufferSize);
}
responseStream.Close();
response.Close();
writer.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Edit I'm sorry for the error in previous code.
When correcting my previous code I found the following resource useful: example

Categories

Resources