Get tcp port website

I need to get the tcp port of the specified website in IIS 7 and IIS 6 using C #. I have a console application that knows the name of the website. He must find the port on which this website is served.

+3
source share
6 answers

you can get with servervariables Request.ServerVariables ["SERVER_PORT"]

+4
source

I think I can use System.DirectoryServices for IIS 6 and Microsoft.Web.Administration for IIS 7.

+2
source

OK. , , .

global.asax asp.net. . Application_Start , - .

, .

, , , SO " IIS - ?"

+1

IIS 80 ( http ), , .

, IIS 6.0, IIS , , script .

- 65535, , html- wget.

+1

IIS 7; -)

private bool checkPortIsOpen(string portNumer)
    {
        ServerManager serverMgr = new ServerManager();

        int index = 0;
        bool isOpen = true;

        foreach (Site mySite in serverMgr.Sites)
        {
            foreach (Microsoft.Web.Administration.ConfigurationElement binding in mySite.GetCollection("bindings"))
            {
                string protocol = (string)binding["protocol"];
                string bindingInfo = (string)binding["bindingInformation"];

                if (protocol.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    string[] parts = bindingInfo.Split(':');
                    if (parts.Length == 3)
                    {
                        string port = parts[1];
                        if(port.Equals(portNumer.ToString()))
                        {
                            isOpen = false;
                            webSite_portInUse = mySite.Name;
                        }

                    }
                }
                index++;

            }
        }
        return isOpen;
    }
+1

, , , , .

, Metabase IIS, IIS6 XML-.

IIS6 systemroot\system32\inetserv\metabase.xml node

//MBProperty/IIsWebServer [@ServerComment = $WebSiteName]/serverBindings

In IIS7, get the file systemroot \ system32 \ inetserv \ config \ applicationHost.config (this is xml, despite the extension .config) and look at node /configuration/system.applicationHost/sites/site [@ name = '$ WebSiteName']

0
source

Source: https://habr.com/ru/post/1748597/


All Articles