我在.NEt 2.0中使用c#來簡單地嘗試上傳文件。在代碼中一切看起來都不錯,但是當我從FtpWebRequest.GetRequestStream方法創建流時,它仍然失敗。我的ftp代碼有什麼問題?
這裏是代碼...
FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse;
try
{
string fileName = Path.GetFileName(strCompleteFilePath);
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://myhost/" + fileName));
ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpRequest.Proxy = null;
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential("myUserID", "myPW");
ftpRequest.KeepAlive = false;
FileInfo ff = new FileInfo(strCompleteFilePath);
byte[] fileContents = new byte[ff.Length];
using (FileStream fr = ff.OpenRead())
{
fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
}
using (Stream writer = ftpRequest.GetRequestStream())
{
writer.Write(fileContents, 0, fileContents.Length);
}
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
}
和錯誤....
{System.Net.WebException: The remote server returned an error: (501) Syntax error in parameters or arguments.
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.InvokeRequestCallback(Object obj)
at System.Net.CommandStream.Abort(Exception e)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetRequestStream()
對不起,文件名WAS在那裏。發佈時我不小心拿出了它。但是,當我運行代碼時,它失敗了,/ IS在那裏。 – donde 2010-05-05 15:47:43
什麼是服務器?這裏有一個關於AS/400的問題可能會有所幫助:http://stackoverflow.com/questions/1930787/ftpwebrequest-connecting-to-an-as-400 – 2010-05-05 16:37:39