Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

UriBuilder does not add default http:// scheme if source uri string contains port number

$
0
0

Hi all,

I am trying to use UriBuilder to parse service address, using constructor as defined in http://msdn.microsoft.com/en-us/library/vstudio/y868d5wh(v=vs.100).aspx :

public UriBuilder(
	string uri
)

Documentation remarks:

This constructor initializes a new instance of the UriBuilder class with the Fragment, Host, Path, Port, Query, Scheme, and Uri properties set as specified in uri.
If uri does not specify a scheme, the scheme defaults to "http:".

But if the uri string is in the form of "hostname:port", default scheme is not added.


Test app:

class Program
{
  static void Main(string[] args)
  {
    TestUriBuilderFromString("http://msdn.microsoft.com/");
    TestUriBuilderFromString("https://msdn.microsoft.com");
    TestUriBuilderFromString("msdn.microsoft.com/");
    TestUriBuilderFromString("msdn.microsoft.com:8080/");
    TestUriBuilderFromString("localhost:1234/");

    Console.ReadLine();
  }

  private static void TestUriBuilderFromString(string uriStr)
  {
    var uri = new UriBuilder(uriStr).Uri;
    Console.WriteLine("Absolute URI from '{0}':  '{1}'", uriStr, uri.AbsoluteUri);
  }
}


Test app output:

Absolute URI from 'http://msdn.microsoft.com/':  'http://msdn.microsoft.com/'
Absolute URI from 'https://msdn.microsoft.com':  'https://msdn.microsoft.com/'
Absolute URI from 'msdn.microsoft.com/':  'http://msdn.microsoft.com/'
Absolute URI from 'msdn.microsoft.com:8080/':  'msdn.microsoft.com:8080/'
Absolute URI from 'localhost:1234/':  'localhost:1234/'

Am I missing something? Is it a correct behaviour, and if it is, are there workarounds to add default scheme ("http://") when the uri string is in the form of "hostname:port"?

Best regards,

Ivan




Viewing all articles
Browse latest Browse all 8156

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>