日期:2014-05-18  浏览次数:21310 次

ftp上传时出错
C# code

private bool CheckFtp(string DomainName, String port, string FtpUserName, string FtpUserPwd)
        {
            try
            {
                FtpWebRequest ftprequest = (FtpWebRequest)WebRequest.Create("ftp://" + DomainName + ":" + port + "/");
                ftprequest.Credentials = new NetworkCredential(FtpUserName, FtpUserPwd);
                ftprequest.Method = WebRequestMethods.Ftp.ListDirectory;
                ftprequest.Timeout = 3000;
                FtpWebResponse ftpResponse = (FtpWebResponse)ftprequest.GetResponse();
                ftpResponse.Close();
                return true;
            }
            catch (Exception ex)
            {
                //提示连接错误信息
                MessageBox.Show(ex.Message);
                return false;
            }
        }


//检测能否连接ftp
            if (CheckFtp(ftpIp, port, userName, userPassword))
            {
                 //上传操作
                 。。。。。。
                 。。。。。。
             }



问题如下:
第一次上传,全部传完,OK没问题,紧接着再传下一批,出问题了。提示“远程服务器返回错误(501),参数或变量中有语法错误”,连续尝试两三次后,又可以传了。后来我试了下,第一批传完后,如果等待1分钟左右,就不会报这个错。不知道是什么原因,请高手帮帮忙。谢谢了。

------解决方案--------------------
private bool CheckFtp(string DomainName, String port, string FtpUserName, string FtpUserPwd)
{
try
{
FtpWebRequest ftprequest = (FtpWebRequest)WebRequest.Create("ftp://" + DomainName + ":" + port + "/");
ftprequest.Credentials = new NetworkCredential(FtpUserName, FtpUserPwd);
//ftprequest.Method = WebRequestMethods.Ftp.ListDirectory;
//ftprequest.Timeout = 3000;
// FtpWebResponse ftpResponse = (FtpWebResponse)ftprequest.GetResponse();
//ftpResponse.Close();
return true;
}
catch (Exception ex)
{
//提示连接错误信息
MessageBox.Show(ex.Message);
return false;
}
}


//检测能否连接ftp
if (CheckFtp(ftpIp, port, userName, userPassword))
{
//上传操作
。。。。。。
。。。。。。
}
这样就Ok了
------解决方案--------------------
探讨
谢谢Vivi,但是我不太需要上传的代码啊,我就是想在上传前先检测下,看看能不能连接上服务器!

------解决方案--------------------
string ftpServerIP;

string ftpUserID;

string ftpPassword;

FtpWebRequest reqFTP;

private void Connect(String path)//连接ftp

{

// 根据uri创建FtpWebRequest对象

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));

// 指定数据传输类型

reqFTP.UseBinary = true;

// ftp用户名和密码

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

}