如何获取IP地址和端口号c#

编程入门 行业动态 更新时间:2024-10-25 00:24:41
本文介绍了如何获取IP地址和端口号c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在C#中获取IP地址和端口号? 好​​的,让IP地址变得简单但是获取端口是个问题。 我在IIS中发布了一些服务,我需要获取有关我正在使用的特定服务的信息。 例如,如果http中使用IIS中的服务,我需要获取端口80,但如果https使用IIS中的服务,则需要获取端口443.

解决方案

我知道了,这是在IIS中获取协议和网站端口的代码: 使用 Microsoft.Web.Administration;

ServerManager serverManager = new ServerManager(); // 获取网站(例如默认) 网站site = serverManager.Sites.FirstOrDefault(s = > s.Name == 默认网站); // 协议hhtp,https .... Console.WriteLine(site.Bindings.ElementAt( 0 )。协议); // Puerto Console.WriteLine(site.Bindings .ElementAt( 0 )。EndPoint.Port); Console.ReadKey();

How to get IP address and Port number in C#? Ok, getting IP addres its easy but getting the port is the problem. I have some services published in IIS and I need to get the information about the especific service I'm consuming. For example if the service in IIS is used by http, I need to get the port 80 but if the service in IIS is used by https I need to get the port 443.

解决方案

I got it, this is the code to get the protocol used and port of the site in IIS:

using Microsoft.Web.Administration;

ServerManager serverManager = new ServerManager(); //get the site (e.g. default) Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "Default Web Site"); //Protocol hhtp, https.... Console.WriteLine(site.Bindings.ElementAt(0).Protocol); //Puerto Console.WriteLine(site.Bindings.ElementAt(0).EndPoint.Port); Console.ReadKey();

更多推荐

如何获取IP地址和端口号c#

本文发布于:2023-08-07 13:45:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319376.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口号   地址   IP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!