ftp远程服务器返回错误,FtpWebRequest返回“远程服务器返回错误:(530)未登录”...

我不完全知道你的问题的解决方案。但一些建议:

尽可能在IDisposable上使用using(...)。这可以在您完成后促进适当的资源释放和清理。 MSDN: Using

您使用的超时时间为6000毫秒,您是否应该增加它对于大文件或使用本地变量timeout(从您的应用程序设置读取)。

改进代码using:

private void FtpTransfer(string siteName, string portNumber, string ftpUser, string ftpPassword, string destPath)

{

DateTime now = DateTime.Now;

string now_string =

(now.Year).ToString()

+ "_" +

(now.Month).ToString("0#")

+ "_" +

(now.Day).ToString("0#");

foreach (object item in listBox1.Items)

{

string srcFile = item.ToString();

lblSource.Text = srcFile;

Uri uri = new Uri(srcFile);

string destFile = srcFile.Replace(lblPath.Text, "").Replace("\\\\", "\\").Replace("\\", "/").Replace("www/", "");

Configuration oConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

int timeout = int.Parse(oConfig.AppSettings.Settings["TimeOut"].Value);

if (siteName == "mysite1.co.in" || siteName == "sd1.mysite2.net")

destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + "/_test" + destFile; //error here

else

destFile = "ftp://" + siteName + ":" + portNumber + "/" + siteName + destFile; //no error

lblDestn.Text = destFile;

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(destFile);

request.Credentials = new NetworkCredential(ftpUser, ftpPassword);

request.Timeout = 6000;

request.Method = WebRequestMethods.Ftp.UploadFile;

request.UsePassive = true;

request.UseBinary = true;

request.KeepAlive = true;

// Copy the contents of the file to the request stream.

byte[] fileContents;

using (StreamReader sourceStream = new StreamReader(@srcFile))

{

fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

}

request.ContentLength = fileContents.Length;

using (Stream requestStream = request.GetRequestStream())

{

requestStream.Write(fileContents, 0, fileContents.Length);

}

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())

{

string path = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);

System.IO.StreamWriter w = System.IO.File.AppendText(path + "\\log_" + now_string + ".txt");

w.WriteLine(DateTime.Now.ToString("yyy-MM-dd HH:mm:ss")

+ " "

+ srcFile

+ " "

+ destFile

+ " "

+ response.StatusDescription);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值