获取客户端机器的IP地址 [英] Get IP address of client machine

查看:115
本文介绍了获取客户端机器的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the IP address of client machine using C#. I am using the below code to get the IP address :

string IPAddress = HttpContext.Current.Request.UserHostAddress;

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

But it is giving me the response in encoded format i.e fe80::ed13:dee2:127e:1264%13

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

How can I get the actual IP address? Any one faced this issue please share some idea.

推荐答案

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

希望这会有所帮助

这篇关于获取客户端机器的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆