title: The underlying connection was closed: The server committed a protocol violation
body: <p>First off all sorry for the trivial question I am a newbie and still learning. I am using vb.net to build an application that connect into a FTP Server list the files and filter this files according with the file extension. Then download and delete the file.</p>
  
  <p>I am getting the error: "The underlying connection was closed: The server committed a protocol violation"</p>
  
  <p>Here is where I call the methods.</p>
  
  <pre><code>Call GetDirectoryListing(strExtension, strPastaDownload, strUser, strPsswd)
          strFileNumber = Cont_Files
          Dim blnDownloaded As Boolean = False
          If Cont_Files &gt; 0 Then
              For cont = 0 To Cont_Files - 1
                  nameFile = arrArq(cont)
                  strFilename = UCase(nameFile)
                  strLocal = AppPath &amp; strLocalArquivos &amp; "\" &amp; nameFile
                  blnDownloaded = mFTP.FtpDownloadFile(strPastaDownload, strNomedoArquivo, strLocal, strUsuario, strSenha)
                  StrDir = Dir(strdirectory)
                  strRemote = nameFile
                  If StrDir &lt;&gt; "" Then
                      FileCopy(AppPath &amp; strfiledirectory &amp; "\" &amp; Trim(strRemote), strLocalBackup &amp; "\" &amp; Trim(strRemote))
                      mFTP.FtpDeleteFile(strServer, strPastaFtp, strFilename, strUsuario, strSenha)
                  End If
              Next
  </code></pre>
  
  <p>Here is the function to list the files I am not getting any errors here just to illustrate </p>
  
  <pre><code>  Public Function GetDirectoryListing(ByVal sFilter As String, ByVal strServer As String, ByVal strLogin As String, ByVal strPassword As String) As ArrayList
  
      Dim ftpRequest As FtpWebRequest = Nothing
      Try
          ftpRequest = FtpWebRequest.Create(New Uri(strServer))
          With ftpRequest
              .Credentials = New NetworkCredential(strLogin, strPassword)
              .UsePassive = True
              .Method = WebRequestMethods.Ftp.ListDirectory
              .KeepAlive = True
          End With
          'ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory  'List, not Download
          'Read the contents of the directory and store it into a string array
          Dim srReader As New StreamReader(ftpRequest.GetResponse().GetResponseStream())
          sDirectoryList = srReader.ReadToEnd().Split(vbNewLine)
          For i As Integer = 0 To UBound(sDirectoryList) - 1
              If sFilter = Mid(sDirectoryList(i), Len(sDirectoryList(i)) - 2, 3) Then
                  listaArquivo.Add(sDirectoryList(i))
              End If
  
          Next
  
          'Clear the last split vbNewLine
          If sDirectoryList IsNot Nothing Then
              sDirectoryList(sDirectoryList.Length - 1) = ""
          End If
  
          srReader.Close()
          srReader = Nothing
          GetDirectoryListing = listaArquivo
          ftpRequest = Nothing
  
  
      Catch ex As WebException
          MessageBox.Show("Failed to get directory listing. GetDirectoryListing()")
          'MessageBox.Show(ex.ToString())
      End Try
  
  End Function
  </code></pre>
  
  <p>Here is the function to download </p>
  
  <pre><code>Public Function FtpDownloadFile(ByVal strUrl As String, ByVal strFileName As String, ByVal strTargetDirectory As String, ByVal strLogin As String, ByVal strPassword As String) As Boolean
  
          Dim ftpRequest As FtpWebRequest = Nothing
          Dim ftpResponse As FtpWebResponse = Nothing
          Dim strFtpFile As String = ""
          Dim strTargetFile As String = ""
          Dim strmReader As StreamReader = Nothing
          Dim strmResponse As Stream = Nothing
          Dim strmTarget As Stream = Nothing
          Dim strmWriter As StreamWriter = Nothing
  
          strFtpFile = strUrl &amp; "/" &amp; Trim(strFileName)
          strTargetFile = strTargetDirectory
  
          Try
              ftpRequest = CType(WebRequest.Create(strFtpFile), FtpWebRequest)
  
              With ftpRequest
                  .Credentials = New NetworkCredential(strLogin, strPassword)
                  .UsePassive = True
                  .Method = WebRequestMethods.Ftp.DownloadFile
                  .KeepAlive = True
              End With
  
              ftpResponse = CType(ftpRequest.GetResponse(), FtpWebResponse) 'line where I am getting the error
  
              strmTarget = New FileStream(strTargetFile, FileMode.Create)
              strmWriter = New StreamWriter(strmTarget)
              strmResponse = ftpResponse.GetResponseStream()
              strmReader = New StreamReader(strmResponse, System.Text.Encoding.UTF8)
              strmWriter.Write(strmReader.ReadToEnd())
  
          Catch ex As Exception
              Throw ex
  
          Finally
          If strmReader IsNot Nothing Then
              strmReader.Close()
              strmReader.Dispose()
          End If
  
          If strmWriter IsNot Nothing Then
              strmWriter.Close()
              strmWriter.Dispose()
          End If
  
          If ftpResponse IsNot Nothing Then
              ftpResponse.Close()
              ftpResponse = Nothing
          End If
  
  
          If ftpRequest IsNot Nothing Then ftpRequest = Nothing
      End Try
  
  
      Return True
  
  End Function
  </code></pre>
  
  <p>Thanks in advance for your help</p>