Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created August 23, 2013 07:30
Show Gist options
  • Save yemrekeskin/6316501 to your computer and use it in GitHub Desktop.
Save yemrekeskin/6316501 to your computer and use it in GitHub Desktop.
Test Connect and Ftp Uri fo FtpClients
public static class FtpTestHelper
{
public static bool TestConnect(string ftpRequestUrl, string ftpUserName, string ftpPassword)
{
bool rv = false;
if (!TestUri(ftpRequestUrl))
return false;
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpRequestUrl));
request.Credentials = new NetworkCredential(ftpUserName, ftpRequestUrl);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
rv = true;
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
rv = true;
}
else
{
rv = false;
}
}
return rv;
}
public static bool TestUri(string ftpUrl)
{
// ftp://DomainName.com/
// ftp://12.12.12.12/
Regex regex = new Regex("^(ftp)://.+$");
return regex.IsMatch(ftpUrl);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment