简体   繁体   English

如何检查FTP连接?

[英]How to check FTP connection?

Is there a simple, fast way to check that a FTP connection (includes host, port, username and password) is valid and working?有没有一种简单、快速的方法来检查 FTP 连接(包括主机、端口、用户名和密码)是否有效和工作? I'm using C#.我正在使用 C#。 Thank you.谢谢你。

try something like this:尝试这样的事情:

FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create("ftp://ftp.google.com");
requestDir.Credentials = new NetworkCredential("username", "password");
try
{
     WebResponse response = requestDir.GetResponse();
     //set your flag
}
catch
{
}

this is the method I use, let me know if you know a better one.这是我使用的方法,如果您知道更好的方法,请告诉我。

/*Hola Este es el metodo que utilizo si conoces uno mejor hasmelo saber Ubirajara 100% Mexicano isc.erthal@gmail.com */ /*Hola Este es el metodo que utilizo si conoces uno mejor hasmelo saber Ubirajara 100% 墨西哥 isc.erthal@gmail.com */

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;
}

This might be useful.这可能有用。

  public async Task<bool> ConnectAsync(string host, string user, string password)
    {
        try
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(host);
            request.Credentials = new NetworkCredential(user, password);
            request.UseBinary = true;
            request.UsePassive = true;
            request.KeepAlive = false; // useful when only to check the connection.
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = (FtpWebResponse) await request.GetResponseAsync();
            return true;
        }
        catch (Exception)
        {
            return false;
        }            
    }

Use either System.Net.FtpWebRequest or System.Net.WebRequestMethods.Ftp to test your connection using your login credentials.使用System.Net.FtpWebRequestSystem.Net.WebRequestMethods.Ftp使用您的登录凭据测试您的连接。 If the FTP request fails for whatever reason the appropriate error message will be returned indicating what the problem was (authentication, unable to connect, etc...)如果 FTP 请求因任何原因失败,将返回相应的错误消息,指出问题所在(身份验证、无法连接等...)

You could try using System.Net.FtpWebRequest and then just check the GetResponseStream method.您可以尝试使用System.Net.FtpWebRequest ,然后检查GetResponseStream方法。

So something like所以像

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
}

This is from the msdn site to diplay files from a server这是来自 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","janeDoe@contoso.com");
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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM