C# .NET - how to get http port number,server location and server port of a web site using CSharp application

Asked By krishna choudary on 12-Oct-07 03:16 AM
Hi all I have a requirement to get the http port number,Server location and web server port in which the web site is running using CSharp.Any web reference or code help is appreciated. Regards Krish

This is backwards. - Peter Bromberg replied to krishna choudary on 12-Oct-07 06:14 AM

You *Need* to have the location, and tcp port of a site before you can connect to it.

If you don't have this information, then how do you propose to "magically" come into it?

how to get http port number,server location and server port of a web site using CSharp application

krishna choudary replied to Peter Bromberg on 12-Oct-07 07:13 AM
The IP address of the site is there with me. I want to know how to get http port number,Server location and web server port in which the web site is running using CSharp

See this - SP replied to krishna choudary on 28-Jun-08 05:47 AM

I know that I can get the SERVER port number like this:

HttpContext.Current.Request.ServerVariables["SERVER_PORT"]

HttpContext.Current.Request.UserHostAddress or Request.UserHostAddress if you're working inline code.
GOOD DAY SIR - SHAIK SHOAIB replied to krishna choudary on 13-Feb-09 10:03 PM

HELLO SIR I AM STUDENT OF APPLICATION SIDE AND I AM DOING MY PROJECT ON C#,TO KNOW THE IP ADDRESS AND PORT  NUMBER TO FIND.SO Please HELP ME WITH SORCE CODE OF C#

                                                                                                         THANK YOU

IP ADDRESS - kss rao replied to SHAIK SHOAIB on 17-Feb-09 06:54 AM

using System.Net;

for people working in .net 1.1

// To Get the hostname

string getHost = System.Net.Dns.GetHostName();

// Display the hostname

MessageBox.Show(getHost);

// To Get the IP from the host name

string getIP = System.Net.Dns.GetHostByName(getHost).AddressList[0].ToString();

// Display the IP

MessageBox.Show(getIP);


For .net 2.0

repalce GetHostByName by GetHostEntry: