Gulrej
Gulrej

Reputation: 999

Get port protocol details from URL

I need to get port protocol details from a URL. Can some one please help me? I have tried with some code -

 Uri uri = new Uri("http://stackoverflow.com/questions/ask");
        string requested = uri.Scheme + Uri.SchemeDelimiter + uri.Host + ":" + uri.Port;

Problem 1 - This code does not tell me whether this is TCP or UDP or ISMP

Problem 2 - This code does not check the actual port/protocol details of the URL.

Problem 3 - I am not able to build the URL if I have URL like this - "stackoverflow.com".

Please some one help me.

Thanks Gulrej

Upvotes: 0

Views: 1180

Answers (2)

Hiral Shah
Hiral Shah

Reputation: 647

This may help, It worked for me

string url = "http://www.contoso.com:8080/letters/readme.html";

 Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
                          RegexOptions.None, TimeSpan.FromMilliseconds(150));
      Match m = r.Match(url);
      if (m.Success)
         Console.WriteLine(r.Match(url).Result("${proto}${port}")); 

Upvotes: 3

Alex Nguyen
Alex Nguyen

Reputation: 1080

With url = stackoverflow.com/questions/ask

You're easily to know :

Protocol = http or https.

And

Port = default port = 80 (http) or 443 (https)

In case having the specific port,it will be placed after hostname like this [stackoverflow.com:8180]

Upvotes: 0

Related Questions