Jessy
Jessy

Reputation: 55

How to get my real IP using vb.net?

How to get my real IP using vb.net?

Upvotes: 2

Views: 3449

Answers (4)

Johnine
Johnine

Reputation: 100

Create php script. Save it as realip.php

<?php
echo $this->getRealIpAddr();


function getRealIpAddr()
    {
        $ip = "";
        if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
        {
          $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
        {
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
            $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
?>

In your VB.net project create a module. Declare the imports section at the very top

Imports System.Net
Imports System.IO

And create your function:

    Public Function GetIP() As String

    Dim uri_val As New Uri("http://yourdomain.com/realip.php")
    Dim request As HttpWebRequest = HttpWebRequest.Create(uri_val)

    request.Method = WebRequestMethods.Http.Get

    Dim response As HttpWebResponse = request.GetResponse()
    Dim reader As New StreamReader(response.GetResponseStream())
    Dim myIP As String = reader.ReadToEnd()

    response.Close()

    Return myIP
End Function

Now anywhere in your code you can issue

Dim myIP as String = GetIP()

as use the value from there as you wish.

Upvotes: 1

Khalid Rahaman
Khalid Rahaman

Reputation: 2302

Dim req As HttpWebRequest = WebRequest.Create("http://whatismyip.com/automation/n09230945.asp")        
Dim res As HttpWebResponse = req.GetResponse()
Dim Stream As Stream = res.GetResponseStream()
Dim sr As StreamReader = New StreamReader(Stream)

MsgBox(sr.ReadToEnd())

Upvotes: 0

goenning
goenning

Reputation: 6654

As you can see here (how and why), the best way to get the client IP is:

Dim clientIP As String
Dim ip As String = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If Not String.IsNullOrEmpty(ip) Then
    Dim ipRange As String() = ip.Trim().Split(","C)
    Dim le As Integer = ipRange.Length - 1
    clientIP = ipRange(le)
Else
    clientIP = Request.ServerVariables("REMOTE_ADDR")
End If

Upvotes: 0

Peter Olson
Peter Olson

Reputation: 142911

If you are running in ASP.NET, then use the HttpRequest.UserHostAddress property:

Dim ip as string
ip = Request.UserHostAddress()

Upvotes: 2

Related Questions