Get “The remote server returned an error: (500) Syntax error, command unrecognized” when I try to run FtpWebRequest.GetRequestStream

为君一笑 提交于 2019-11-28 06:03:46

问题


I have following code to send files to a FTP server.

function FtpUploader(
  [string]$uri,
  [string]$localeFile,
  [string]$user = "ftp",
  [string]$password = "ftp",
  [int]   $timeout  = 20000
){
    trap {
      Write-Host ("ERROR: " + $_) -Foregroundcolor Red
      return $false 
    }

    $ftp             = [System.Net.FtpWebRequest]::Create($uri)
    $ftp             = [System.Net.FtpWebRequest]$ftp
    $ftp.Method      = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $ftp.Credentials = new-object System.Net.NetworkCredential($user, $password)  
    $ftp.Timeout     = $timeout
    $ftp.UseBinary   = $false
    $ftp.UsePassive  = $true

    $content         = Get-Content -en byte $localeFile

    $rs              = $ftp.GetRequestStream()
    $rs.Write($content, 0, $content.Length)

    $rs.Close()
    $rs.Dispose()

    return $true
}

The URI I use is "ftp://xxx.xxx.xxx.xxx/aaa/bbb/ccc/R1ACTIVE.TXT". The FTP server is vsftpd

Most of the time, the file is uploaded. But sometime I get following error when it try to run $ftp.GetRequestStream():

The remote server returned an error: (500) Syntax error, command unrecognized.

Why???


回答1:


I solved it by using the following:

$ftp.KeepAlive = $false


来源:https://stackoverflow.com/questions/2067313/get-the-remote-server-returned-an-error-500-syntax-error-command-unrecogniz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!