2015-09-28 147 views
-1

我是新來的,編寫一個C#winforms FTP程序來上傳文件。下面是我在「上傳」按鈕下編寫的代碼,在運行該程序時出現「無法連接到服務器」錯誤。請幫忙。FTP上傳 - 無法連接到遠程服務器

private void button2_Click(object sender, EventArgs e) 
    { 
     string ftpServerIP = textBox2.Text; 
     string filename = textBox1.Text; 
     string ftpUserID = "Administrator"; 
     string ftpPassword = "admin"; 
     FileInfo fileInf = new FileInfo(filename); 
     string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name; 
     FtpWebRequest reqFTP; 

     // Create FtpWebRequest object from the Uri provided 
     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
        "ftp://" + ftpServerIP + "/" + fileInf.Name)); 

     // Provide the WebPermission Credintials 
     reqFTP.Credentials = new NetworkCredential(ftpUserID, 
                ftpPassword); 

     // By default KeepAlive is true, where the control connection is 
     // not closed after a command is executed. 
     reqFTP.KeepAlive = false; 

     // Specify the command to be executed. 
     reqFTP.Method = WebRequestMethods.Ftp.UploadFile; 

     // Specify the data transfer type. 
     reqFTP.UseBinary = true; 

     // Notify the server about the size of the uploaded file 
     reqFTP.ContentLength = fileInf.Length; 

     // The buffer size is set to 2kb 
     int buffLength = 2048; 
     byte[] buff = new byte[buffLength]; 
     int contentLen; 

     // Opens a file stream (System.IO.FileStream) to read 
     // the file to be uploaded 
     FileStream fs = fileInf.OpenRead(); 

     try 
     { 
      // Stream to which the file to be upload is written 
      Stream strm = reqFTP.GetRequestStream(); 

      // Read from the file stream 2kb at a time 
      contentLen = fs.Read(buff, 0, buffLength); 

      // Till Stream content ends 
      while (contentLen != 0) 
      { 
       // Write Content from the file stream to the 
       // FTP Upload Stream 
       strm.Write(buff, 0, contentLen); 
       contentLen = fs.Read(buff, 0, buffLength); 
      } 

      // Close the file stream and the Request Stream 
      strm.Close(); 
      fs.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Upload Error"); 
     } 
    } 
} 
} 

輸出窗口:

Output Window: 'FileTransfer.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'FileTransfer.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'c:\users\administrator\documents\visual studio 2015\Projects\28sept\FileTransfer\FileTransfer\bin\Debug\FileTransfer.exe'. Symbols loaded. 
'FileTransfer.exe' (CLR v4.0.30319: FileTransfer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'FileTransfer.exe' (CLR v4.0.30319: FileTransfer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'FileTransfer.exe' (CLR v4.0.30319: FileTransfer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'FileTransfer.exe' (CLR v4.0.30319: FileTransfer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'FileTransfer.exe' (CLR v4.0.30319: FileTransfer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'FileTransfer.exe' (CLR v4.0.30319: FileTransfer.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
Exception thrown: 'System.Net.WebException' in System.dll 
The program '[15776] FileTransfer.exe' has exited with code 0 (0x0). 
The program '[15776] FileTransfer.exe: Program Trace' has exited with code 0 (0x0). 
+0

請提及你所在錯誤的行和你的錯誤的堆棧跟蹤 –

+0

這裏是我得到的地方出現錯誤:「Stream strm = reqFTP.GetRequestStream();」並請指導我在何處獲取VS2015中的堆棧跟蹤 –

+0

在您的輸出窗口中。從那裏複製整個錯誤並將其粘貼到此處。 –

回答

0

,您將需要正常化您的憑據:

reqFTP.Credentials = new NetworkCredential(ftpUserID.Normalize(), ftpPassword.Normalize(),ftpDomain.Normalize()); 

(在情況下,如果你有一個不同的域名來訪問ftp站點)