我目前收到以下錯誤:
遠程服務器返回錯誤:(501)參數或參數中的語法錯誤 .
我已經檢查了服務器并且文件確實存在,如果我打開命令提示符并鍵入以下代碼它可以工作:
ftp
open 192.168.1.2
cd /Images
get S12345.jpeg
這是正常的,但是一旦我嘗試通過此代碼連接:
private bool DownloadPod(string server)
{
string[] allocate = server.Split('\\');
string ftp = @"ftp://192.168.1.2/Images/" + allocate.Last();
Uri uri = new Uri(ftp);
// The code path for uri: ftp://192.168.1.2/Images/S12345.jpeg
var request = WebRequest.Create(uri) as FtpWebRequest;
if(request != null)
{
request.Method = WebRequestMethods.Ftp.DownloadFile;
// Left credentials off for security.
request.Credentials = new NetworkCredential(@"", @"");
// The line that triggers the error (response)
using(FtpWebResponse response = request.GetResponse() as FtpWebResponse)
using(Stream stream = response.GetResponseStream())
using(StreamReader reader = new StreamReader(stream))
{
reader.ReadToEnd();
return true;
}
}
return false;
}
有人可以向我解釋為什么這不起作用?
憑據在命令提示符下工作
在服務器上物理文件
可以從命令提示符下載
根據MSDN:
要獲取FtpWebRequest的實例,請使用Create方法 . 您還可以使用WebClient類從FTP服務器上載和下載信息 . 使用這些方法中的任何一種,當您指定使用FTP方案的網絡資源(例如,“ftp://contoso.com”)時,FtpWebRequest類提供以編程方式與FTP服務器交互的功能 . URI可以是相對的或絕對的 . 如果URI的格式為“ftp://contoso.com/%2fpath”(%2f是轉義'/'),那么URI是絕對的,當前目錄是/ path . 但是,如果URI的格式為“ftp://contoso.com/path”,則首先.NET Framework登錄到FTP服務器(使用Credentials屬性設置的用戶名和密碼),然后是當前目錄設置為/ path .
這就是AS400期望數據通過的方式 .