2017-06-01 131 views
0

我可以創建一個文件夾,我可以重命名該文件,但我無法上傳和下載文件到ftp。顯示的異常是System.Net.WebException:遠程服務器返回錯誤:(500)語法錯誤,命令無法識別。沒有連接到ftp使用c#

任何人都有一個想法,爲什麼?

日誌文件: System.Net Information: 0 : [6112] FtpControlStream#7746814 - Resposta recebida [500 PORT/EPRT (Active Mode/Extended Active Mode) is not supported. Use PASV/EPSV instead of this] System.Net Information: 0 : [6112] FtpWebRequest#30923613::(Liberando a conexão de FTP#7746814.) System.Net Error: 0 : [6112] Exceção em FtpWebRequest#30923613::GetRequestStream - O servidor remoto retornou um erro: (500) Erro de sintaxe, comando não reconhecido.

代碼:

/* Upload File */ 
public void upload(string remoteFile, string localFile) 
{ 
    try 
    { 
     /* Create an FTP Request */ 
     ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + remoteFile); 
     /* Log in to the FTP Server with the User Name and Password Provided */ 
     ftpRequest.Credentials = new NetworkCredential(user, pass); 
     /* When in doubt, use these options */ 
     ftpRequest.UseBinary = true; 
     ftpRequest.UsePassive = false; 
     ftpRequest.KeepAlive = true; 

     /* Specify the Type of FTP Request */ 
     ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; 
     /* Establish Return Communication with the FTP Server */ 
     Stream ftpStream = ftpRequest.GetRequestStream(); 
     /* Open a File Stream to Read the File for Upload */ 
     FileStream localFileStream = new FileStream(localFile, FileMode.Create); 
     /* Buffer for the Downloaded Data */ 
     byte[] byteBuffer = new byte[bufferSize]; 
     int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize); 
     /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */ 
     try 
     { 
      while (bytesSent != 0) 
      { 
       ftpStream.Write(byteBuffer, 0, bytesSent); 
       bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize); 
      } 
     } 
     catch (Exception ex) { Console.WriteLine(ex.ToString()); } 
     /* Resource Cleanup */ 
     localFileStream.Close(); 
     ftpStream.Close(); 
     ftpRequest = null; 
    } 
    catch (Exception ex) { Console.WriteLine(ex.ToString()); } 
    return; 
} 
+0

的可能的複製[定影 - System.Net.WebException:遠程服務器返回錯誤:(500)語法錯誤,不可識別的命令(https://stackoverflow.com/questions/21221300/fixing-system-net- webexception-the-remote-server-returned-an-error-500-syn) –

+0

Ie使用'ftpRequest.UsePassive = true;',除非你有一個非常好的**理由不要。 –

+0

如果我使用ftpRequest.UsePassive = true;顯示的異常是:遠程服務器返回一個錯誤:227進入被動模式(201,23,75,26,225,86) –

回答

0

我的問題是,擋住通信端口卡巴斯基殺毒。

+0

請不要發佈重複的答案:https://stackoverflow.com/a/24261401/850848 - 你應該用Google搜索答案,提問前! –