西西软件下载最安全的下载网站、值得信赖的软件下载站!

首页编程开发C#.NET → c#获得服务器的外网ip、内网ip

c#获得服务器的外网ip、内网ip

相关软件相关文章发表评论 来源:西西整理时间:2012/12/6 11:08:16字体大小:A-A+

作者:玉开Sir点击:0次评论:0次标签: ip

  • 类型:音频处理大小:1M语言:中文 评分:5.3
  • 标签:
立即下载

如下代码可以获得服务器所在内网的内网ip

IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
    if (ip.AddressFamily.ToString() == "InterNetwork")
    {
        localIP = ip.ToString();
        break;
    }
}
return localIP;

如果要获得服务器的公网ip,可以使用下面代码

public string GetPublicIP()
{
    String direction = "";
    WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
    using (WebResponse response = request.GetResponse())
    using (StreamReader stream = new StreamReader(response.GetResponseStream()))
    {
        direction = stream.ReadToEnd();
    }

    //Search for the ip in the html
    int first = direction.IndexOf("Address: ") + 9;
    int last = direction.LastIndexOf("</body>");
    direction = direction.Substring(first, last - first);

    return direction;
}

    相关评论

    阅读本文后您有什么感想? 已有人给出评价!

    • 8 喜欢喜欢
    • 3 顶
    • 1 难过难过
    • 5 囧
    • 3 围观围观
    • 2 无聊无聊

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)