問題描述
我正在嘗試通過 FTP 以編程方式下載 C# 中的文件,這里是相關代碼(顯然帶有假憑據和 URI):
I'm trying to programatically download a file in C# via FTP, here is the relevant code (obviously with fake credntials and URI):
try
{
var request = FtpWebRequest.Create("ftp://ftp.mydomain.com/folder/file.zip");
request.Credentials = new NetworkCredential("username", "password");
using (var response = request.GetResponse())
{
...
}
}
catch (WebException we)
{
...
}
request.GetResponse()
處引發異常,錯誤代碼為 550.問題不在于憑據或 URI,因為它們在 IE 中運行良好并且文件下載成功在那里.我錯過了什么?我應該使用其他類型的憑證對象嗎?request
對象上是否有我未設置的屬性?任何幫助將不勝感激.
The exception is thrown at request.GetResponse()
, and the error code is 550. The issue is not with the credentials or the URI, as they work just fine in IE and the file downloads successfully there. What am I missing? Is there some other kind of credential object I should be using? Is there a property on the request
object I'm not setting? Any help would be appreciated.
推薦答案
原來FTP根不一定和URL根一樣.也許我混淆了術語,所以讓我解釋一下:在我的例子中,連接到 ftp.mydomain.com 已經從/folder 開始,所以我的 URL 只需 ftp://ftp.mydomain.com/file.zip.IE8 知道如何消除原始路徑中多余的/folder 部分,而 FtpRequest 類不知道,這就是為什么它在 IE8 中有效,但在 C# 代碼中無效.
Turns out the FTP root isn't necessarily the same as the URL root. Perhaps I'm mixing up terminology, so let me explain: in my case, connecting to ftp.mydomain.com already starts at /folder, so my URL needed to just be ftp://ftp.mydomain.com/file.zip. IE8 knows how to eliminate the redundant /folder part in the original path while the FtpRequest class does not, which is why it worked in IE8 but not in the C# code.
這篇關于C# FTP 550 錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!