Search code examples
ftpftpwebrequestsocketexception

Uploading file to FTP with VB.NET


I'm getting errors when trying to upload a file to FTP Server with the next VB.NET code:

Dim miUri As String = "ftp://ftp.mydomain.com/folder/file.jpg"
Dim miRequest As Net.FtpWebRequest = Net.WebRequest.Create(miUri)
miRequest.Credentials = New Net.NetworkCredential("user", "pass")
miRequest.Method = Net.WebRequestMethods.Ftp.UploadFile
Try
    Dim bFile() As Byte = System.IO.File.ReadAllBytes("C:\carpeta\fichero.jpg")
    Dim miStream As System.IO.Stream = miRequest.GetRequestStream()
    miStream.Write(bFile, 0, bFile.Length)
    miStream.Close()
    miStream.Dispose()
Catch ex As Exception
    Throw New Exception(ex.Message & ". El Archivo no pudo ser enviado.")
End Try

ex.Message = "Error on remote server: 227 Entering Passive Mode (x,x,x,x,21,183). ." ex.InnerException.Message = System.Net.Sockets.SocketException = {"A socket operation was attempted to an unreachable network x.x.x.x:5557"}

The line of code that throws the exception is:

Dim miStream As System.IO.Stream = miRequest.GetRequestStream()

POINTS:

  • If I try to connect by FileZilla or other FTP Client, I can connect without problems.

  • If I disable the antivirus, I can connect without problems.

  • Before somebody says something related to the firewall...

IF THE ANTIVIRUS IS ON, AND I CONNECT BY FILEZILLA, I CAN CONNECT WITHOUT PROBLEMS.

¿Where is the problem? ¿What I have to do in my code to get it run with the antivirus ON? If FileZilla can, I also have to be able...

Thanks a lot for your replies. Regards,


Solution

  • Sorry for the issue.

    Finally it was because of the antivirus version I had installed.

    Even putting the application as a trusted application, antivirus continued blocking the application.

    I installed another version of the same antivirus and everything works fine.

    Regards.