Kumar S
Kumar S

Reputation: 133

parsing an url in c# without protocol instead with port number

When I use Uri class to parse a url like e.g. client-lb.dropbox.com:443, Uri class couldn't parse the value and get the correct results such as url.Port : 443, url.Host = client-lb.dropbox.com.

 var urlValue = "client-lb.dropbox.com:443";
 var url = new Uri(urlValue);
 Console.WriteLine("Host : {0}, Port {1} ", url.Host, url.Port);

Result :

Host : , Port -1

How can i fix this using Uri class ? Any suggestions are appreciated ..

Upvotes: 1

Views: 4400

Answers (3)

Kumar S
Kumar S

Reputation: 133

I have re-written this parser and thanks to Trotinet project for the base implementation.

private static void ParseUrl(string url)
        {
            string host = null;
            var port = 123;
            var prefix = 0; 
            if (url.Contains("://"))
            {
                if (url.StartsWith("http://"))
                    prefix = 7; // length of "http://"
                else if (url.StartsWith("https://"))
                {
                    prefix = 8; // length of "https://"
                    port = 443;
                }
                else
                {
                    throw new Exception("Expected scheme missing or unsupported");
                }
            }

            var slash = url.IndexOf('/', prefix);
            string authority = null;
            if (slash == -1)
            {
                // case 1
                authority = url.Substring(prefix);
            }
            else
                if (slash > 0)
                    // case 2
                    authority = url.Substring(prefix, slash - prefix);

            if (authority != null)
            {
                Console.WriteLine("Auth is " + authority);
                // authority is either:
                // a) hostname
                // b) hostname:
                // c) hostname:port

                var c = authority.IndexOf(':');
                if (c < 0)
                    // case a)
                    host = authority;
                else
                    if (c == authority.Length - 1)
                        // case b)
                        host = authority.TrimEnd('/');
                    else
                    {
                        // case c)
                        host = authority.Substring(0, c);
                        port = int.Parse(authority.Substring(c + 1));
                    }
            }
            Console.WriteLine("The Host {0} and Port : {1}", host, port);
        }

Upvotes: 0

Manas
Manas

Reputation: 2542

Url should look like [protocol]://hostname:[port], by default for https port is 443 and for http port is 80.

Protocols have information about their default port. But port cannot associate them with a protocol.

Upvotes: 0

Kien Chu
Kien Chu

Reputation: 4895

var urlValue = "http://client-lb.dropbox.com:443";
var url = new Uri(urlValue);
Console.WriteLine("Host : {0}, Port {1} ", url.Host, url.Port);

Response:

Host : client-lb.dropbox.com, Port 443 

Upvotes: 4

Related Questions