Why do I get 'System.UriFormatException: Invalid URI: Invalid port specified.' when using an IPv6 URI?

StackOverflow https://stackoverflow.com/questions/20402684

  •  29-08-2022
  •  | 
  •  

Question

Why does this var uri = new Uri("ftp://1111:2222:3333::43/testing/1kb.zip");

Throw this exception?

System.UriFormatException: Invalid URI: Invalid port specified. at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)

Was it helpful?

Solution

From RFC 2732:

To use a literal IPv6 address in a URL, the literal address should be enclosed in "[" and "]" characters.

For example, this works fine:

var uri = new Uri("ftp://[1111:2222:3333::43]/testing/1kb.zip");

If you want to specify the port, it needs to be outside the square brackets:

var uri = new Uri("ftp://[1111:2222:3333::43]:100/testing/1kb.zip");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top