How to get my real IP using vb.net?
How to get my rea开发者_如何学编程l IP using vb.net?
If you are running in ASP.NET, then use the HttpRequest.UserHostAddress property:
Dim ip as string
ip = Request.UserHostAddress()
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.
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
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())
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论