以下代码在request.GetRequestStream()部分失败。我们收到“(503)错误的命令顺序”
var fileNameAndFtpPath = Path.Combine(ConfigurationManager.AppSettings["FtpPath"], Path.GetFileName(fileNameAndLocalPath));
byte[] fileContents;
using (StreamReader sourceStream = new StreamReader(fileNameAndLocalPath))
fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fileNameAndFtpPath);
request.KeepAlive = false;
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.EnableSsl = true;
request.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["FtpUserName"], String.Empty);
request.Proxy = new WebProxy();
request.ContentLength = fileContents.Length;
request.EnableSsl = true;
X509CertificateCollection certCollection = new X509CertificateCollection();
certCollection.Add(GetCertificate());
request.ClientCertificates = certCollection;
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(fileContents, 0, fileContents.Length);
当我们检查登录过程时,它看起来像这样:
FtpControlStream#64844482 - Sending command [USER xxx]
... (certificate stuff)
FtpControlStream#64844482 - Received response [232 User xxx logged in]
FtpControlStream#64844482 - Sending command [PASS]
FtpControlStream#64844482 - Received response [503 You are already logged in]
看起来不应该发送PASS命令,但是如何强制FtpWebRequest这样做呢?