Created
August 23, 2013 07:30
-
-
Save yemrekeskin/6316501 to your computer and use it in GitHub Desktop.
Test Connect and Ftp Uri fo FtpClients
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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