This project is read-only.

URI: how to for critical characters (ex. "@" or ":")

Oct 1, 2012 at 4:57 PM

My application must connect to a uri similiar to this:
ftp://n@me:password@ftp.site.com/files/
I wrote this code:

String uriString = "ftp://n@me:password@ftp.site.com/files/";
Uri uri = new Uri(uriString);
System.Net.FtpClient.FtpClient fc = new System.Net.FtpClient.FtpClient(uri);
fc.Connect();

If I run this code I get an error during creation of the new Uri(..) because it can't recognize that uri.
If I try to connect with Firefox the first character "@" in previous uriString is replaced by "%40". Then I tried the same code of before with this change:

String uriString = "ftp://n%40me:password@ftp.site.com/files/";
Uri uri = new Uri(uriString);
System.Net.FtpClient.FtpClient fc = new System.Net.FtpClient.FtpClient(uri);
fc.Connect();

Now there is not any error during creation of new Uri but fc.Connect fails.
I replaced originally username and password check with an "UrlDecode" that permits a correct login procedure.
After debugging I've found a solution; I've modified source code in this way:

String uriString = "ftp://n%40me:password@ftp.site.com/files/";
Uri uri = new Uri(uriString);
System.Net.FtpClient.FtpClient fc = new System.Net.FtpClient.FtpClient(uri);
fc.Username = System.Web.HttpUtility.UrlDecode(fc.Username);
fc.Password = System.Web.HttpUtility.UrlDecode(fc.Password);
fc.Connect();

That code permit to parse special character into a uri string.
I hope that this can be helpful for someone.