System.Net.WebException:远程服务器返回错误:(530)未登录

时间:2014-05-30 14:47:27

标签: c# asp.net ftp

我遇到与此问题相同的问题:using ftpWebRequest with an error: the remote server returned error 530 not logged in

我在那里尝试了解决方案,但我无法超越它。

// Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://<ftp-ip>/uploadFTP.txt");
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.UsePassive = false;
            request.EnableSsl = true;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential(<username>, <password>);
            request.Timeout = -1;

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader(<file to be upload>); 
            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();

            //Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

            response.Close();

我可能在哪里出错?

1 个答案:

答案 0 :(得分:2)

我删除了request.timeout = -1。而且,还有UsePassive和EnableSsl,现在它工作正常。感谢。

我不确定问题是什么,但这是最终的工作代码:

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://<ip>/<file_name>");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential("username", "password");

        // Copy the contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader("<file_name>");

        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();
        response.Close();