在 vb.net 中获取客户端 IP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12393995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get client IP in vb.net
提问by Amritpal singh
I am trying to get client ID with vb.net. My code is as under :
我正在尝试使用 vb.net 获取客户端 ID。我的代码如下:
Partial Class foo
Inherits System.Web.UI.Page
Private ipv4 As New clsIPv4
Dim ref As String = 0
Dim client_ip As String
client_ip = Request.UserHostAddress()
Dim myHost As String = System.Net.Dns.GetHostName()
client_ip = ipv4.ResolveAddress(myHost)
This code is returning me list of ip adresses (i.e 182.50.130.143;118.139.172.1;118.139.172.2;118.139.172.3;118.139.172.4;118.139.172.5;118.139.172.6;118.139.172.7;118.139.172.8;118.139.172.9;118.139.172.10;118.139.172.11;118.139.172.12;118.139.172.13;118.139.172.14;118.139.172.15;118.139.172.16;118.139.172.17;118.139.172.18;118.139.172.19;118.139.172.20;118.139.172.21;118.139.172.22;118.139.172.23;118.139.172.24;118.139.172.25;118.139.172.26;118).
此代码返回我的IP不会忽略的列表(即182.50.130.143; 118.139.172.1; 118.139.172.2; 118.139.172.3; 118.139.172.4; 118.139.172.5; 118.139.172.6; 118.139.172.7; 118.139.172.8; 118.139.172.9 ; 118.139.172.10; 118.139.172.11; 118.139.172.12; 118.139.172.13; 118.139.172.14; 118.139.172.15; 118.139.172.16; 118.139.172.17; 118.139.172.18; 118.139.172.19; 118.139.172.20; 118.139.172.21; 118.139 .172.22;118.139.172.23;118.139.172.24;118.139.172.25;118.139.172.26;118)。
Please guide where I am wrong
请指导我错在哪里
回答by Karamafrooz
If you are using ASP.NET, you can use: Request.UserHostAddress
to get your client IP address.
如果您使用的是 ASP.NET,您可以使用:Request.UserHostAddress
来获取您的客户端 IP 地址。
But if you are using a windows application to get local IP address, in fact you may receive multiple IP addresses! In this case, if you want to get a specific address which you know the netid part of the IP address you can compare all IP addresses with a corresponding netid address.
但是如果你使用windows应用程序获取本地IP地址,实际上你可能会收到多个IP地址!在这种情况下,如果您想获得一个您知道 IP 地址的 netid 部分的特定地址,您可以将所有 IP 地址与相应的 netid 地址进行比较。
回答by user2953513
This worked perfectly for me
这对我来说非常有效
System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).GetValue(0).ToString()
System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()).GetValue(0).ToString()