简体   繁体   English

如何在Windows应用商店应用中获取本地主机IP地址?

[英]How do I get the local host IP address in a Windows Store app?

A particular rest service I am calling in a Windows Store app wants the IP address of the calling computer as a parameter. 我正在Windows Store应用程序中调用的特定休息服务需要调用计算机的IP地址作为参数。 None of the tried and true examples using System.Net or System.Net.NetworkInformation compile in a Store app. 使用System.NetSystem.Net.NetworkInformation的所有经过验证的真实示例都无法在Store应用程序中编译。

What is the magical combination of types and methods available in a windows store app that will get to the local machine's IP address? Windows应用商店中可获取本地计算机IP地址的类型和方法的神奇组合是什么? I feel like it is probably obvious but I am not seeing it! 我觉得它可能很明显,但我没有看到它!

You will have to contact an external server. 您将必须联系外部服务器。 Even if the platform supplies an API to retrieve the network address, the host could still be located behind a proxy or a NAT (and you will see something like 192.168.1.4, instead of your external IP address). 即使平台提供了用于检索网络地址的API,主机仍可能位于代理服务器或NAT之后(并且您会看到类似192.168.1.4的信息,而不是外部IP地址)。

Just perform an HTTP request towards services like http://ifconfig.me/ or http://whatismyip.com/ and parse the IP. 只需对诸如http://ifconfig.me/http://whatismyip.com/之类的服务执行HTTP请求并解析IP。

Local network IP address: 本地网络IP地址:

foreach (HostName localHostName in NetworkInformation.GetHostNames())
{
    if (localHostName.IPInformation != null)
    {
        if (localHostName.Type == HostNameType.Ipv4)
        {
            // E.g.: 192.168.1.108
            Debug.WriteLine(localHostName);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM