How to check FTP connection?

我怕爱的太早我们不能终老 提交于 2019-12-18 04:48:17

问题


Is there a simple, fast way to check that a FTP connection (includes host, port, username and password) is valid and working? I'm using C#. Thank you.


回答1:


You could try using System.Net.FtpWebRequest and then just check the GetResponseStream method.

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
}



回答2:


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



回答3:


/*Hola Este es el metodo que utilizo si conoces uno mejor hasmelo saber Ubirajara 100% Mexicano 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;
        }



回答4:


Use either System.Net.FtpWebRequest or System.Net.WebRequestMethods.Ftp to test your connection using your login credentials. 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...)




回答5:


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 _ftpRequest.GetResponseAsync();
            return true;
        }
        catch (Exception)
        {
            return false;
        }            
    }



回答6:


This is from the msdn site to diplay files from a server

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


来源:https://stackoverflow.com/questions/3234242/how-to-check-ftp-connection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!