C# 使用ftpWebRequest时出错:远程服务器返回错误530未登录
我正在尝试使用c#中的ftpWebRequest 我的代码是C# 使用ftpWebRequest时出错:远程服务器返回错误530未登录,c#,ftpwebrequest,C#,Ftpwebrequest,我正在尝试使用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;
// 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();
我在request.GetRequestStream()中得到一个错误;
错误是:远程服务器返回错误530未登录
如果我尝试进入浏览器页面并在url中写入
浏览页面要求我输入姓名和密码,我输入了相同的姓名和密码,我看到ftp文件夹中的所有文件和文件夹。行不应该:
request.Credentials = new NetworkCredential("dev\ftp", "devftp");
是:
或:
我不得不相信这可能会导致您的问题,因为f是一个表单馈送字符。我发现通过.NET/C连接到FTP服务器时,某些字符是无效的。在将登录凭据设置为仅限字母和数字[a-Z0-9]后,可以登录。面临同样的问题,我的解决方案如下:
request.Credentials = new NetworkCredential(
usernameVariable.Normalize(),passwordVariable.Normalize(),domainVariable.Normalize());
详情见
希望有帮助。请删除用户名或密码中的双引号,因为有时$sign-in用户名或密码会导致问题。每次使用单引号很好。如果假设匿名登录不设置网络凭据。这是我问题的根源。不要对几个问题给出相同的答案。也许最好是将它们标记为彼此的副本。这很有效!谢谢
request.Credentials = new NetworkCredential(@"dev\ftp", "devftp");
request.Credentials = new NetworkCredential(
usernameVariable.Normalize(),passwordVariable.Normalize(),domainVariable.Normalize());