0

Topic: FTP file transfer over SSL

im trying to upload file to one of the ftp servers I have but im getting exception while reaching the server server listening on port 990 i think the problem is with implicit/explicit settings can anyone suggest me how to do the ftp over SSL using implicit setting in .net 4.0?

The exception I am getting while calling the method GetRequestStream: The underlying connection was closed: The server committed a protocol violation.

Here is the code snippet:

FileInfo ExportFile = new FileInfo("C:\\ServiceXML.xml");
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftpserver" + "/FolderName/" + ExportFile.Name);
request.EnableSsl = true;
request.Credentials = new NetworkCredential("username", "password");
request.KeepAlive = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UseBinary = true;
request.ContentLength = ExportFile.Length;
request.UsePassive = true;
ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };

        try
        {
            int bufferLen = 4096;
            byte[] buffer = new byte[4096];
            int contentLen;

            FileStream fsExport = ExportFile.OpenRead();
            Stream ftpStream = request.GetRequestStream();

            contentLen = fsExport.Read(buffer, 0, bufferLen);

            while (contentLen != 0)
            {
                ftpStream.Write(buffer, 0, contentLen);
                contentLen = fsExport.Read(buffer, 0, bufferLen);
            }

            fsExport.Close();
            fsExport.Dispose();
            ftpStream.Close();
            ftpStream.Dispose();
        }
        catch (Exception ex)
        {
            //LogError(ex, "Error in FTP Request.");
        }

Struggling with this issue for few days now..any help would be appreciated.. can anyone suggest me good example for FTP over SSL using implict settings in .net 4.0?

pibcat
  • 94
  • 2
  • 9
  • what happens when you try over plain ftp? This could be a ssl cert error issue (which can be bypassed if you want to chance it), but I just want to confirm. – bob-the-destroyer Feb 04 '13 at 02:16
  • I tried request.EnableSSL = false; it gave me same error :( – pibcat Feb 04 '13 at 02:39
  • From my development environment I tried FireZilla to connect to server and upload files which was successful..no clue whats wrong with my code.. – pibcat Feb 04 '13 at 02:42
  • Can you check the response with something like Fiddler2 to see what the server is saying? – Billdr Feb 04 '13 at 03:01
  • i tried to see it in fiddler but request is not hitting the server..i didn't see anything in fiddler related to my request.. – pibcat Feb 04 '13 at 03:02
  • .NET Framework just doesn't support implicit SSL mode. You need some third-party component to do the job. Our SecureBlackbox or Rebex are good choices. – Eugene Mayevski 'Callback Feb 04 '13 at 04:37

2 Answers2

0

Had similar issue except I was getting "Local error in processing" on .net 4. Changed framework to .net 3.5 and it worked.

royc
  • 1
-1

I had the same problem when I wasn't setting the port in the URI, your ftpserver variable should be something like ftp://127.0.0.1:990

Vasfed
  • 18,013
  • 10
  • 47
  • 53