A easy way to use C# to Parse a URL

来源:互联网 发布:菊花泡水后变绿 知乎 编辑:程序博客网 时间:2024/05/21 06:41

I find it from here, and it is a good solution. 


using System;using System.Web; //need to change the property of the project target framework into 4, not clientusing System.Collections.Specialized;namespace test1{    class Program    {        static void Main(string[] args)        {            Uri tmp = new Uri("http://www.google.co.uk/search?hl=en&q=parsing+a+url+in+c%23&aq=f&aqi=g1g-j9&aql=&oq=");                        Console.WriteLine("Protocol: {0}", tmp.Scheme);            Console.WriteLine("Host: {0}", tmp.Host);            Console.WriteLine("Path: {0}", HttpUtility.UrlDecode(tmp.AbsolutePath));            Console.WriteLine("Query: {0}", tmp.Query);            NameValueCollection Parms = HttpUtility.ParseQueryString(tmp.Query);            Console.WriteLine("Parms: {0}", Parms.Count);            foreach (string x in Parms.AllKeys)                Console.WriteLine("\tParm: {0} = {1}", x, Parms[x]);            Console.ReadLine();        }    }}




原创粉丝点击