3

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)

user247702
  • 22,915
  • 14
  • 108
  • 152
Shawn
  • 2,307
  • 4
  • 47
  • 79

1 Answers1

10

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");
Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
  • Jon, i read this http://stackoverflow.com/a/1984225/22656 and am confused why following is wrong new Uri("Prerak/Kaushik/SomeResource"); Thanks. – Prerak K Feb 11 '14 at 11:26
  • 3
    @PrerakK: This sounds like it should be a new question. In particular, it has *nothing* to do with this question about IPv6 representations. (But in brief: you haven't specified a URI scheme name in there.) – Jon Skeet Feb 11 '14 at 11:27