The following code throws System.UriFormatException:
var uri = new UriBuilder("ftp://user:pass#[email protected]:21/fu/bar.zip");
System.UriFormatException: Invalid URI: A port was expected because of there is a colon (':') present but the port could not be parsed.
Removing the # symbol from the password field solves the issue.
Thanks, Andrew
A URI is a compact representation of a resource available to your application on the intranet or internet. The Uri class defines the properties and methods for handling URIs, including parsing, comparing, and combining. The Uri class properties are read-only; to create a modifiable object, use the UriBuilder class.
A URI is a string containing characters that identify a physical or logical resource. URI follows syntax rules to ensure uniformity. Moreover, it also maintains extensibility via a hierarchical naming scheme. The full form of URI is Uniform Resource Identifier.
You should be able to use %23 instead.
The percent symbol followed by a two digit hex number is how characters are escaped in URLs. 23 is the hexadecimal value for the hash/pound symbol in the ASCII table.
Rather than solving this particular problem, you should solve this problem generally by encoding the whole username and password fields. You should be able to do this with System.Web.HttpUtility.UrlEncode
(reference the System.Web
assembly):
string username = ...
string password = ...
string url = string.Format("ftp://{0}:{1}@ftp.example.com:21/fu/bar.zip", HttpUtility.UrlEncode(username),
HttpUtility.UrlEncode(password));
The # would need to be encoded, since it's considered a special character. Even then, not sure it would work. Never tried.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With