C# 使用 ftpWebRequest 时出现错误:远程服务器返回错误 530 未登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10352165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:30:56  来源:igfitidea点击:

using ftpWebRequest with an error: the remote server returned error 530 not logged in

c#ftpwebrequest

提问by user1361207

I am trying to use the ftpWebRequest in c# my code is

我正在尝试在 c# 中使用 ftpWebRequest 我的代码是

// Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://192.168.20.10/file.txt");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("dev\ftp", "devftp");

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader(@"\file.txt");
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
            request.UsePassive = true;
            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();

and I get an Error in request.GetRequestStream(); the error is: the remote server returned error 530 not logged in if I try to go in to a browser page and in the url I write ftp://192.168.20.10/the brows page is asking me for a name and password, I put the same name and password and I see all the files and folders in the ftp folder.

我在 request.GetRequestStream() 中遇到错误;错误是:如果我尝试进入浏览器页面,则远程服务器返回错误 530 未登录,并且在 url 中我写ftp://192.168.20.10/浏览页面 要求我输入名称和密码,我输入相同的名称和密码,我会看到 ftp 文件夹中的所有文件和文件夹。

回答by Matthew Doyle

Shouldn't the line:

不应该是:

request.Credentials = new NetworkCredential("dev\ftp", "devftp");

be:

是:

request.Credentials = new NetworkCredential("dev\ftp", "devftp");

or:

或者:

request.Credentials = new NetworkCredential(@"dev\ftp", "devftp");

I have to believe this may be causing your issues because the \f is a form feed character.

我不得不相信这可能会导致您的问题,因为 \f 是换页符。

回答by user1820536

I found out that when connecting via .NET/C# to a FTP-server some characters are not "valid". After setting the login credentials to only only letters and numbers [a-Z0-9] it worked to log in.

我发现当通过 .NET/C# 连接到 FTP 服务器时,某些字符不是“有效”的。将登录凭据设置为仅字母和数字 [a-Z0-9] 后,它就可以登录了。

回答by Godwin

Faced the same issue, here's my solution:

遇到同样的问题,这是我的解决方案:

request.Credentials = new NetworkCredential(
    usernameVariable.Normalize(),passwordVariable.Normalize(),domainVariable.Normalize());

Details can be found here

详细信息可以在这里找到

Hope it helps.

希望能帮助到你。

回答by Eagle

Just remove the double quote from username or password because sometimes $ sign in username or password causes problem. Its good to use single quote every time.

只需从用户名或密码中删除双引号,因为有时 $ 登录用户名或密码会导致问题。每次都使用单引号很好。

回答by helpME1986

If you assume anonymous logon do not set network credentials. this was the root of my problems.

如果您假设匿名登录,请不要设置网络凭据。这是我问题的根源。