Uri

Instances of Uri have read-only properties.

To modify an existing Uri, instantiate a UriBuilder object-this has writable properties and can be converted back via its Uri property.

Uri also provides methods for comparing and subtracting paths:


using System;
using System.Net;

using System.Linq;
using System.Text;

class Program
{
    static void Main()
    {
        Uri info = new Uri("http://www.domain.com:80/info/");
        Uri page = new Uri("http://www.domain.com/info/page.html");

        Console.WriteLine(info.Host); // www.domain.com
        Console.WriteLine(info.Port); // 80
        Console.WriteLine(page.Port); // 80 

        Console.WriteLine(info.IsBaseOf(page)); // True 
        Uri relative = info.MakeRelativeUri (page); 
        Console.WriteLine (relative.IsAbsoluteUri); // False
    }
}

The output:


www.domain.com
80
80
True
False

The following downloads and displays the code samples web page:


using System;
using System.Net;
using System.IO;
using System.Linq;
using System.Text;

class Program
{
    static void Main()
    {
        WebRequest req = WebRequest.Create("http://www.google.com");
        req.Proxy = null;
        using (WebResponse res = req.GetResponse()) 
        using (Stream s = res.GetResponseStream()) 
        using (StreamReader sr = new StreamReader(s))
            File.WriteAllText("code.html", sr.ReadToEnd());

        System.Diagnostics.Process.Start("code.html");
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.