Parsi Coders

نسخه‌ی کامل: تجزیه یک آدرس اینترنتی (Uri parsing)
شما در حال مشاهده نسخه آرشیو هستید. برای مشاهده نسخه کامل کلیک کنید.
با استفاده از اين ابع شما مي توانيد يك آدرس اينترنتي را تجزيه كنيد

چند نكته :‌

1- اين كد فقط در .net 3 جواب خواهد داد

2- فضاي نام system.net.uri بايد اضافه شود
کد:
public static void ParseUri(Uri uri)

        {

            try

            {

                // System.Net.Uri class constructor has parsed it for us.

                // new Uri("http://user:password@localhost:8080/www.abc.com/

                // home%20page.htm?item=1233#stuff")

                StringBuilder uriParts = new StringBuilder();

                uriParts.AppendFormat("AbsoluteURI: {0}{1}",

            uri.AbsoluteUri, Environment.NewLine);

                uriParts.AppendFormat("AbsolutePath: {0}{1}",

                uri.AbsolutePath, Environment.NewLine);

                uriParts.AppendFormat("Scheme: {0}{1}",

                uri.Scheme, Environment.NewLine);

                uriParts.AppendFormat("UserInfo: {0}{1}",

                uri.UserInfo, Environment.NewLine);

                uriParts.AppendFormat("Authority: {0}{1}",

                uri.Authority, Environment.NewLine);

                uriParts.AppendFormat("DnsSafeHost: {0}{1}",

            uri.DnsSafeHost, Environment.NewLine);

                uriParts.AppendFormat("Host: {0}{1}",

                uri.Host, Environment.NewLine);

                uriParts.AppendFormat("HostNameType: {0}{1}",

                uri.HostNameType.ToString(), Environment.NewLine);

                uriParts.AppendFormat("Port: {0}{1}", uri.Port, Environment.NewLine);

                uriParts.AppendFormat("Path: {0}{1}", uri.LocalPath, Environment.NewLine);

                uriParts.AppendFormat("QueryString: {0}{1}", uri.Query, Environment.NewLine);

                uriParts.AppendFormat("Path and QueryString: {0}{1}",

                uri.PathAndQuery, Environment.NewLine);

                uriParts.AppendFormat("Fragment: {0}{1}", uri.Fragment, Environment.NewLine);

                uriParts.AppendFormat("Original String: {0}{1}",

                uri.OriginalString, Environment.NewLine);

                uriParts.AppendFormat("Segments: {0}", Environment.NewLine);

                for (int i = 0; i < uri.Segments.Length; i++)

                    uriParts.AppendFormat(" Segment {0}:{1}{2}",

                    i, uri.Segments[i], Environment.NewLine);

                // GetComponents can be used to get commonly used combinations

                // of URI information.

                uriParts.AppendFormat("GetComponents for specialized combinations: {0}",

                Environment.NewLine);

                uriParts.AppendFormat("Host and Port (unescaped): {0}{1}",

                uri.GetComponents(UriComponents.HostAndPort,

                UriFormat.Unescaped), Environment.NewLine);

                UriParts.AppendFormat("HttpRequestUrl (unescaped): {0}{1}",

                uri.GetComponents(UriComponents.HttpRequestUrl,

                UriFormat.Unescaped), Environment.NewLine);

                UriParts.AppendFormat("HttpRequestUrl (escaped): {0}{1}",

                uri.GetComponents(UriComponents.HttpRequestUrl,

                UriFormat.UriEscaped), Environment.NewLine);

                UriParts.AppendFormat("HttpRequestUrl (safeunescaped): {0}{1}",

                uri.GetComponents(UriComponents.HttpRequestUrl,

                UriFormat.SafeUnescaped), Environment.NewLine);

                UriParts.AppendFormat("Scheme And Server (unescaped): {0}{1}",

                uri.GetComponents(UriComponents.SchemeAndServer,

                UriFormat.Unescaped), Environment.NewLine);

                UriParts.AppendFormat("SerializationInfo String (unescaped): {0}{1}",

                uri.GetComponents(UriComponents.SerializationInfoString,

                UriFormat.Unescaped), Environment.NewLine);

                UriParts.AppendFormat("StrongAuthority (unescaped): {0}{1}",

                uri.GetComponents(UriComponents.StrongAuthority,

                UriFormat.Unescaped), Environment.NewLine);

                UriParts.AppendFormat("StrongPort (unescaped): {0}{1}",

                uri.GetComponents(UriComponents.StrongPort,

                UriFormat.Unescaped), Environment.NewLine);

                Console.WriteLine(UriParts.ToString());

            }

            catch (ArgumentNullException e)

            {

                Console.WriteLine("Uri string object is a null reference: {0}", e);

            }

            catch (UriFormatException e)

            {

                Console.WriteLine("Uri formatting error: {0}", e);

            }

        }