是否有一種簡單而快速的方式來檢查FTP連接(包括主機,端口,用戶名和密碼)是否有效並正常工作?我正在使用C#。謝謝。如何檢查FTP連接?
回答
你可以嘗試使用 System.Net.FtpWebRequest
,然後只檢查GetResponseStream
方法返回的FTP請求失敗。
因此,像
System.Net.FtpWebRequest myFTP = new System.Net.FtpWebRequest
//Add your credentials and ports
try
{
myFTP.GetResponseStream();
//set some flags
}
catch ex
{
//handle it when it is not working
}
你的代碼是不會編譯的 – sarsnake 2012-09-22 00:44:45
使用System.Net.FtpWebRequest或System.Net.WebRequestMethods.Ftp使用您的登錄憑證來測試您的連接。如果無論什麼原因,相應的錯誤信息會指出哪些問題是(身份驗證,無法連接等)
嘗試這樣的事:
FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.google.com");
requestDir.Credentials = new NetworkCredential("username", "password");
try
{
WebResponse response = requestDir.GetResponse();
//set your flag
}
catch
{
}
我已經實現了類似於你的解決方案anishMarokey的東西,但是這對於安全的ftp站點來說似乎失敗了。您對如何測試ftps連接有任何瞭解嗎?謝謝。 – 2011-09-29 14:42:03
對於此代碼的工作,您還需要設置請求方法(即requestDir.Method = WebRequestMethods.Ftp.ListDirectoryDetails)。 – laurie 2013-09-25 13:03:49
@ChrisMitchell .NET尚不支持SFTP,但有像[SSH.NET](http://sshnet.codeplex.com/)這樣的庫,具有該功能 – laurie 2013-09-25 13:09:57
/* HOLA 埃斯特ES EL方法方法闕utilizo SI conoces UNO MEJOR hasmelo軍刀 Ubirajara 100%的墨西哥 ISC。 [email protected] */
private bool isValidConnection(string url, string user, string password)
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(user, password);
request.GetResponse();
}
catch(WebException ex)
{
return false;
}
return true;
}
StackOverflow使用英語作爲其主要語言。 – 2015-12-20 04:26:35
這是從MSDN網站從服務器
public static bool DisplayFileFromServer(Uri serverUri)
{
// The serverUri parameter should start with the ftp:// scheme.
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
WebClient request = new WebClient();
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","[email protected]");
try
{
byte [] newFileData = request.DownloadData (serverUri.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
Console.WriteLine(fileString);
}
catch (WebException e)
{
Console.WriteLine(e.ToString());
}
return true;
}
- 1. 如何通過NSTask檢查FTP連接?
- 2. 正確檢查FTP服務器連接
- 3. 檢查一個Python FTP連接
- 4. 如何查看FTP匿名連接
- 5. Android如何連接到FTP?
- 6. 如何檢查websocket連接?
- 7. 如何檢查mysql連接
- 8. 如何檢查PDO連接?
- 9. 檢查FTP連接是否成功或不在Mule任何點?
- 10. Powershell FTP連接
- 11. FTP連接drivehq
- 12. iPhone FTP連接
- 13. 檢測有效的FTP連接
- 14. PHP - 驗證FTP連接 - 檢測FTP超時
- 15. 如何在UWP中創建FTP連接?
- 16. 如何使用C連接到FTP?
- 17. 如何限制FTP連接時間
- 18. iOS:如何減少FTP連接超時?
- 19. 如何關閉一個Zehon FTP連接
- 20. 如何連接到FTP從Android的
- 21. 如何連接FTP服務器?
- 22. 如何從代碼連接到ftp
- 23. 如何檢查Android中的Internet連接?
- 24. 如何檢查android wifi連接?
- 25. 如何檢查連接到php頁面
- 26. Java JDBC連接池(如何檢查)?
- 27. 如何檢查數據連接
- 28. 如何檢查本地wifi連接
- 29. 如何檢查我的svn連接?
- 30. WinRT如何檢查互聯網連接?
一定要接受一個合適的答案,這一個diplay文件。 – 2010-07-13 04:59:49