FTP上传 - 无法连接到远程服务器

时间:2015-09-28 06:01:29

标签: c# winforms ftp

我是新手并编写了一个C#winforms 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).

1 个答案:

答案 0 :(得分:0)

您需要规范化您的凭据:

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

(如果您有不同的域来访问ftp站点)