gpt4 book ai didi

c# - 获取客户端机器的IP地址

转载 作者:可可西里 更新时间:2023-11-01 08:16:11 25 4
gpt4 key购买 nike

我正在尝试使用 C# 获取客户端计算机的 IP 地址。我正在使用以下代码获取 IP 地址:

string IPAddress = HttpContext.Current.Request.UserHostAddress;

但它以编码格式给我响应,即 fe80::ed13:dee2:127e:1264%13

如何获取实际IP地址?任何遇到此问题的人都请分享一些想法。

最佳答案

C#

string IPAddress = GetIPAddress();

public string GetIPAddress()
{
IPHostEntry Host = default(IPHostEntry);
string Hostname = null;
Hostname = System.Environment.MachineName;
Host = Dns.GetHostEntry(Hostname);
foreach (IPAddress IP in Host.AddressList) {
if (IP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
IPAddress = Convert.ToString(IP);
}
}
return IPAddress;
}

VB.net

Dim Host As IPHostEntry
Dim Hostname As String
Hostname = My.Computer.Name
Host = Dns.GetHostEntry(Hostname)
For Each IP As IPAddress In Host.AddressList
If IP.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
IPAddress = Convert.ToString(IP)
End If
Next
Return IPAddress

希望对你有帮助

关于c# - 获取客户端机器的IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21155352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com