您的位置:首页 > 其它

通过拨号连接获取公网IP地址

2010-01-06 09:41 351 查看
很多时候,有需要用到本机公网IP地址,试了很多方法,一直不理想。

在这里有两种情况,一种是本机拨号的,一种是通过路由上网的。

一、如果是通过路由上网的,基本没有什么好办法,只有通过外网上一个页面返回自己的IP地址。

Http是我自己封装httpwebrequest使用的一个类,可以在网上搜到一大把资料。

代码

using System.Net.NetworkInformation;

string re = "";
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
if (ni.NetworkInterfaceType == NetworkInterfaceType.Ppp)
{
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
re= ip.Address.ToString();
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: