如何检查FTP连接?

发布于 2024-09-09 08:29:25 字数 68 浏览 2 评论 0原文

是否有一种简单、快速的方法来检查 FTP 连接(包括主机、端口、用户名和密码)是否有效且正常工作?我正在使用 C#。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笨笨の傻瓜 2024-09-16 08:29:25

尝试这样的事情:

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

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
{
}
静谧幽蓝 2024-09-16 08:29:25

这是我使用的方法,如果您知道更好的方法,请告诉我。

/*霍拉
这是使用最佳哈斯梅洛军刀的方法
乌比拉哈拉 100% 墨西哥
[电子邮件受保护]
*/

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 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
[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;
}
微暖i 2024-09-16 08:29:25

这可能有用。

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

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;
        }            
    }
后eg是否自 2024-09-16 08:29:25

使用 System.Net.FtpWebRequestSystem.Net.WebRequestMethods.Ftp 来测试您的使用您的登录凭据进行连接。如果 FTP 请求由于某种原因失败,将返回相应的错误消息,指示问题所在(身份验证、无法连接等...)

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...)

揽清风入怀 2024-09-16 08:29:25

你可以尝试使用
System.Net.FtpWebRequest 然后只需检查 GetResponseStream 方法。

所以像

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
}

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
}
沙与沫 2024-09-16 08:29:25

这是来自 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;
}

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","[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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文