title: How to build a Url?
body: <p>Are there any helper classes available in .NET to allow me to build a Url?</p>
  
  <p>For example, if a user enters a string:</p>
  
  <pre><code>stackoverflow.com
  </code></pre>
  
  <p>and i try to pass that to an <code>HttpWebRequest</code>:</p>
  
  <pre><code>WebRequest.CreateHttp(url);
  </code></pre>
  
  <p>It will fail, because it is not a valid url (it has no prefix).</p>
  
  <p>What i want is to be able to parse the partial url the user entered:</p>
  
  <pre><code>Uri uri = new Uri(url);
  </code></pre>
  
  <p>and then fix the missing pieces:</p>
  
  <pre><code>if (uri.Port == 0)
     uri.Port = 3333;
  if (uri.Scheme == "")
     uri.Scheme = "https";
  </code></pre>
  
  <p>Does .NET have any classes that can be used to parse and manipulate Uri's?</p>
  
  <h1>The UriBuilder class can't do the job</h1>
  
  <p>The value that the user entered (e.g. <code>stackoverflow.com:3333</code>) is valid; i just need a class to pick it apart. i <em>tried</em> using the <code>UriBuilder</code> class:</p>
  
  <pre><code>UriBuilder uriBuilder = new UriBuilder("stackoverflow.com:3333");
  </code></pre>
  
  <p>unfortunately, the UriBuilder class is unable to handle URIs:</p>
  
  <ul>
  <li>uriBuilder.Path = <code>3333</code></li>
  <li>uriBuilder.Port = <code>-1</code></li>
  <li>uriBuidler.Scheme = <code>stackoverflow.com</code></li>
  </ul>
  
  <p>So i need a class that can understand <code>host:port</code>, which especially becomes important when <a href="http://www.jsonrpc.org/specification" rel="noreferrer">it's not particularly <code>http</code>, but could be</a>.</p>
  
  <h1>Bonus Chatter</h1>
  
  <p>Console application.</p>
  
  <h1>From the other question</h1>
  
  <p>Some examples of URL's that require parsing:</p>
  
  <ul>
  <li><code>server:8088</code> </li>
  <li><code>server:8088/func1</code></li>
  <li><code>server:8088/func1/SubFunc1</code></li>
  <li><code>http://server</code></li>
  <li><code>http://server/func1</code></li>
  <li><code>http://server/func/SubFunc1</code></li>
  <li><code>http://server:8088</code></li>
  <li><code>http://server:8088/func1</code></li>
  <li><code>http://server:8088/func1/SubFunc1</code></li>
  <li><code>magnet://server</code></li>
  <li><code>magnet://server/func1</code></li>
  <li><code>magnet://server/func/SubFunc1</code></li>
  <li><code>magnet://server:8088</code></li>
  <li><code>magnet://server:8088/func1</code></li>
  <li><code>magnet://server:8088/func1/SubFunc1</code></li>
  <li><code>http://[2001:db8::1]</code></li>
  <li><code>http://[2001:db8::1]:80</code></li>
  </ul>
  
  <p>The format of a Url is:</p>
  
  <pre><code>  foo://example.com:8042/over/there?name=ferret#nose
    \_/   \_________/ \__/\_________/\__________/ \__/
     |         |        |     |           |        |
  scheme      host    port   path       query   fragment
  </code></pre>
  
  <h2>Bonus Chatter</h2>
  
  <p>Just to point out again that <strong>UriBuilder</strong> does not work:</p>
  
  <ul>
  <li><a href="https://dotnetfiddle.net/s66kdZ" rel="noreferrer">https://dotnetfiddle.net/s66kdZ</a></li>
  </ul>