Manipulate URIs - CSharp Network

CSharp examples for Network:URL

Description

Manipulate URIs

Demo Code

using System;/*from w  ww. j  a v a2  s .  com*/
class MainClass
{
   private static string defualtUrl = "http://book2s.com/resources/a.png";
   static void Main(string[] args)
   {
      Uri uri = null;
      string strUri = defualtUrl;
      if (args.Length > 0 && !String.IsNullOrEmpty(args[0]))
      {
         strUri = args[0];
      }
      // Safely parse the url
      if (Uri.TryCreate(strUri, UriKind.RelativeOrAbsolute, out uri))
      {
         Console.WriteLine("Parsed URI: " + uri.OriginalString);
         Console.WriteLine("\tScheme: " + uri.Scheme);
         Console.WriteLine("\tHost: " + uri.Host);
         Console.WriteLine("\tPort: " + uri.Port);
         Console.WriteLine("\tPath and Query: " + uri.PathAndQuery);
      }
      else
      {
         Console.WriteLine("Unable to parse URI: " + strUri);
      }
      // Create a new URI.
      UriBuilder newUri = new UriBuilder();
      newUri.Scheme = "http";
      newUri.Host = "www.book2s.com";
      newUri.Port = 80;
      newUri.Path = "resources/a.png";
      Console.WriteLine(Environment.NewLine);
      Console.WriteLine("Created URI: " + newUri.Uri.AbsoluteUri);
   }
}

Result


Related Tutorials