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

时间:2022-03-24 03:46:19

I get this error "The remote server returned an error: (530) Not logged in." while uploading the file using FtpWebRequest.

当使用FtpWebRequest上传文件时,远程服务器返回一个错误:(530)没有登录。

  1. Error occurs only when I transfer the files to path having subfolders, otherwise it perfectly works fine.
  2. 只有当我将文件转移到有子文件夹的路径时,才会发生错误,否则它会完美地工作。
  3. Uploading large files about 5 to 10 MB, it gets timed out.

    上传大约5到10 MB的大文件,会超时。

    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();
        }
    

2 个解决方案

#1


0  

I am assuming that you have already attempted to do the same operation through a standard FTP client. If not, try that. I would then use Wireshark to make sure that is the response coming from the server and verify that the credentails are being sent. After that is verified, check with the FTP owner and make sure the server is configured appropriately.

我假设您已经尝试通过一个标准的FTP客户端执行相同的操作。如果没有,试试。然后我将使用Wireshark来确保这是来自服务器的响应,并验证正在发送邮件。在验证之后,请与FTP所有者进行检查,并确保服务器的配置是适当的。

#2


-2  

I do not exactly know the solution for your problem. but some suggestions:

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

Use using(...) on IDisposable classes when ever possible. This promotes proper resource releasing and cleanup when you are finished with it. MSDN: Using

尽可能在IDisposable类上使用(…)。当您完成资源释放和清理时,这将促进适当的资源释放和清理。MSDN:使用

You use a timeout of 6000 miliseconds, may you should increase it for huge files or use your local variable timeout (read from you app settings).

您使用6000毫秒的超时,您可以为大型文件增加它,或者使用您的本地变量超时(从您的应用程序设置中读取)。

Improved code with 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);

                }
            }

        }

#1


0  

I am assuming that you have already attempted to do the same operation through a standard FTP client. If not, try that. I would then use Wireshark to make sure that is the response coming from the server and verify that the credentails are being sent. After that is verified, check with the FTP owner and make sure the server is configured appropriately.

我假设您已经尝试通过一个标准的FTP客户端执行相同的操作。如果没有,试试。然后我将使用Wireshark来确保这是来自服务器的响应,并验证正在发送邮件。在验证之后,请与FTP所有者进行检查,并确保服务器的配置是适当的。

#2


-2  

I do not exactly know the solution for your problem. but some suggestions:

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

Use using(...) on IDisposable classes when ever possible. This promotes proper resource releasing and cleanup when you are finished with it. MSDN: Using

尽可能在IDisposable类上使用(…)。当您完成资源释放和清理时,这将促进适当的资源释放和清理。MSDN:使用

You use a timeout of 6000 miliseconds, may you should increase it for huge files or use your local variable timeout (read from you app settings).

您使用6000毫秒的超时,您可以为大型文件增加它,或者使用您的本地变量超时(从您的应用程序设置中读取)。

Improved code with 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);

                }
            }

        }