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

问题描述 投票:0回答:2

我收到此错误“远程服务器返回错误:(530) 未登录。”使用 FtpWebRequest 上传文件时。

  1. 仅当我将文件传输到具有子文件夹的路径时才会出现错误,否则它完全可以正常工作。
  2. 上传大约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();
        }
    
c# ftp ftpwebrequest
2个回答
0
投票

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


-2
投票

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

尽可能在

using(...)
课程上使用
IDisposable
。当您完成后,这会促进正确的资源释放和清理。 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);

                }
            }

        }
© www.soinside.com 2019 - 2024. All rights reserved.