简体   繁体   English

检索设备的IP地址

[英]Retrieve IP address of device

I am using Xamarin.Android and wrote the following code: 我正在使用Xamarin.Android并编写以下代码:

public TextView text;
text = FindViewById<TextView>(Resource.Id.viewIP);
foreach (IPAddress adress in Dns.GetHostAddresses(Dns.GetHostName()))
{
    text.Text = "IP Adress: " + adress;
}

However, when I open the application it shuts down immediately. 但是,当我打开应用程序时,它会立即关闭。 Am I using the correct way of getting the IP address' of the device? 我使用正确的方法来获取设备的IP地址吗?

From the Xamarin forums 来自Xamarin论坛

Java.Util.IEnumeration networkInterfaces = NetworkInterface.NetworkInterfaces;

while(networkInterfaces.HasMoreElements)
{
  Java.Net.NetworkInterface netInterface = 
                            (Java.Net.NetworkInterface)networkInterfaces.NextElement();
  Console.WriteLine(netInterface.ToString());
}

added to mainifest: 添加到mainifest:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

for get local ip: 获取本地IP:

public static string GetLocalIPAddress()
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            return ip.ToString();
        }
    }
    throw new Exception("Local IP Address Not Found!");
}

See this answer : https://stackoverflow.com/a/6803109/4349342 请参阅此答案: https//stackoverflow.com/a/6803109/4349342

For me this worked in PCL Xamarin: 对我来说这适用于PCL Xamarin:

public static string GetIPAddress()
{
    var AllNetworkInterfaces = Collections.List(Java.Net.NetworkInterface.NetworkInterfaces);
    var IPAddres = "";
    foreach (var interfaces in AllNetworkInterfaces)
    {
        if (!(interfaces as Java.Net.NetworkInterface).Name.Contains("eth0")) continue;

        var AddressInterface = (interfaces as Java.Net.NetworkInterface).InterfaceAddresses;
        foreach (var AInterface in AddressInterface)
        {
            if(AInterface.Broadcast != null)
                IPAddres = AInterface.Address.HostAddress;
        }
    }
        return IPAddres;
}

All the answers I've seen to this question have only gotten the internal IP address of my device while on my home network (198.162.#.#). 我在这个问题上看到的所有答案都只是在我的家庭网络上获得了我的设备的内部IP地址(198.162。#。#)。 So I took a slightly different approach, and ask the internet more directly. 所以我采取了略微不同的方法,并更直接地询问互联网。 ipify.org has a nice and simple endpoint for getting your IP address, that can be executed in your shared code. ipify.org有一个很好的简单端点来获取你的IP地址,可以在你的共享代码中执行。 For example... 例如...

var client = new HttpClient();
var response = await client.GetAsync("https://api.ipify.org/?format=json");
var resultString = await response.Content.ReadAsStringAsync();

var result = JsonConvert.DeserializeObject<IpResult>(resultString);

var yourIp = result.Ip;

Where "IpResult" is a POCO with a single string property named "Ip" (that you need to create, in addition to this code.) 其中“IpResult”是一个POCO,其中包含一个名为“Ip”的字符串属性(除此代码外,您还需要创建)。

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

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