Re: Default Port Number for a particular "Scheme"?
- From: Jeroen Mostert <jmostert@xxxxxxxxx>
- Date: Fri, 15 May 2009 13:13:45 +0200
Kevin Frey wrote:
Is there a .NET class where I can do something like this:No. However, the following will approximate it:
int defaultPort = SomeDotNetClass.GetDefaultPort( Uri.UriSchemeHttps );
int defaultPort = new Uri(Uri.UriSchemeHttps + "://dummy").Port;
Although this approach produces invalid URIs (for example, "mailto://dummy" isn't a valid URI as the mailto: scheme doesn't allow hierarchies and the "/" characters would need to be escaped to be part of a mailbox name), all the built-in parsers will accept this format as they all support hierarchies.
The Uri class is able to advise default port numbers for various schemes (but does not seem extensible)
You can derive a new UriParser and use UriParser.Register() to associate a default port with a new scheme. There is no way to simply associate a scheme with a port number without providing parsing capabilities, but there doesn't seem to be much use for that either. You could easily write your own class for this, though.
but I don't know whether they have been hard-coded or whether they haveI wouldn't fret over this. The default ports are codified in RFCs. If you compare the effort required to make them changeable to the probability that they'll change within your application's lifetime it's obvious that it doesn't matter if you hard-code them.
been pulled from somewhere else, and I would dearly like to avoid
hard-coding 80, 443 etc in my application.
The solution I presented above is kludgy in that it might break down if you introduce custom parsers that don't allow hierarchies at all, so it has little advantage over simply putting in a
const int HttpDefaultPort = 80;
where needed. In this particular case, it's more important that people know what the magic number 80 means than that it's easy to change. The least redundant solution that's still guaranteed not to break is to write your own specific class to associate schemes with port numbers, but this will be divorced from the framework's own mapping.
--
J.
.
- Follow-Ups:
- Re: Default Port Number for a particular "Scheme"?
- From: Kevin Frey
- Re: Default Port Number for a particular "Scheme"?
- References:
- Default Port Number for a particular "Scheme"?
- From: Kevin Frey
- Default Port Number for a particular "Scheme"?
- Prev by Date: Re: OO design question
- Next by Date: High-efficiency report design with Excel-like reporting tool
- Previous by thread: Default Port Number for a particular "Scheme"?
- Next by thread: Re: Default Port Number for a particular "Scheme"?
- Index(es):
Relevant Pages
|