如何获取IP地址和端口号c# [英] How to get IP address and Port number c#

查看:1164
本文介绍了如何获取IP地址和端口号c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中获取IP地址和端口号?



好​​的,让IP地址变得简单但是获取端口是个问题。



我在IIS中发布了一些服务,我需要获取有关我正在使用的特定服务的信息。

例如,如果http中使用IIS中的服务,我需要获取端口80,但如果https使用IIS中的服务,则需要获取端口443.

解决方案

我知道了,这是在IIS中获取协议和网站端口的代码:



< pre lang =C#> 使用 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#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆