Get IP address of client machine

前端 未结 4 1281
情书的邮戳
情书的邮戳 2020-12-09 11:09

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.Reque         


        
4条回答
  •  温柔的废话
    2020-12-09 11:23

    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
    

    Hope this helps

提交回复
热议问题