远程服务器返回了一个错误:(530)未登录

本文关键字:一个 错误 登录 服务器 返回 | 更新日期: 2023-09-27 17:51:16

在使用FtpWebRequest上传文件时,我得到这个错误"远程服务器返回一个错误:(530)未登录。"


    只有当我将文件传输到具有子文件夹的路径时才会发生错误,否则它会正常工作。
  1. 上传大文件5 ~ 10mb,超时。

    void FtpTransfer(string siteName, string portNumber, string ftpUser, string ftpPassword, string destPath)
    {
        FtpWebRequest request;
        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;
            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.
            StreamReader sourceStream = new StreamReader(@srcFile);
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();
            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);
            w.Close();
            response.Close();
        }
    

远程服务器返回了一个错误:(530)未登录

我假设您已经尝试过通过标准FTP客户机执行相同的操作。如果没有,那就试试。然后,我将使用Wireshark来确保这是来自服务器的响应,并验证正在发送凭据。验证后,与FTP所有者检查并确保服务器配置正确。

我不完全知道你的问题的解决办法。但有些建议:

尽可能在IDisposable类上使用using(...)。这有助于在完成任务后适当地释放和清理资源。MSDN:使用

你使用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);
                }
            }
        }
相关文章: