0

I want to get the IPv4Address but this code return some other IP address that is always the same. How can I get the Users IP address?

Private Function GetIPv4Address() As String

        GetIPv4Address = String.Empty
        Dim strHostName As String = System.Net.Dns.GetHostName()
        Dim iphe As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(strHostName)

        For Each ipheal As System.Net.IPAddress In iphe.AddressList
            If ipheal.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork Then
                GetIPv4Address = ipheal.ToString()
            End If
        Next

        lblIP.Text = GetIPv4Address

    End Function

2 Answers 2

1
Private Function GetExternalIp() As String
    Try
        Dim externalIP As String =New WebClient().DownloadString("http://checkip.dyndns.org/")
        externalIP = (New Regex("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) _
                     .Matches(externalIP)(0).ToString()
        Return externalIP
    Catch
        Return Nothing
    End Try
End Function

Edit:

You never mentioned that this was part of a web site, although I see that it is tagged as ASP .NET now. That makes a HUGE difference. What you are seeing is the IP address of the server that your web site is running on. Therefore it is the same for every visitor. If you want the IPs of individual visitors, then you will need to do it on the client-side using JavaScript. VB (and ASP) .NET only runs on the server side.

This might not be the prettiest solution, but it should work:

function getIp()
{
    var xmlhttp;
    if (window.XMLHttpRequest) 
    {
        xmlhttp = new XMLHttpRequest();
    }
    else 
    {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET","http://api.hostip.info/get_html.php",false);    
    xmlhttp.send();   
    var hostIpInfo = xmlhttp.responseText.split("\n");

    for (i=0; hostIpInfo.length >= i; i++) 
    {
        ipAddress = hostIpInfo[i].split(":");
        if ( ipAddress[0] == "IP" ) 
        {
            return ipAddress[1];
        }
    }
}

Here is a fiddle with the above code

Sign up to request clarification or add additional context in comments.

11 Comments

When I go to this url checkip.dyndns.org directly the IP is different from the code, why?
What does it come up as in the code? Also, are you behind a proxy?
I have no idea about the proxy, its just showing a different address. Only the first 2 numbers in the IP is the same.
Even when I do IPCONFIG on my local PC it shows a different IP than from the url or any other webites that shows your IP
@Etienne Does it show an IP that starts with 192.168 or 10.0? If so, that is your "private IP" (the IP that is internal to your network). The various websites show your "public IP", which is the IP that is exposed to the Internet.
|
0

This ended up being my solution.

<head id="Head1" runat="server">

<script type="application/javascript">

    var myip;
    function ip_callback(o) 
    {
        myip = o.host;
    }

    function Theval() 
    {
        document.getElementById('lblIP').innerText = myip
    }

</script>

<script type="application/javascript" src="https://smart-ip.net/geoip-json?callback=ip_callback"></script>

<title></title>

</head>

<body onload="Theval()">

    <form id="form1" runat="server">

             <asp:Label ID="lblIP" runat="server" Text="lblIP">aaa</asp:Label>

    </form>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.