2015-09-28 98 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站点)