如何使用vb.net获取我的真实IP?

时间:2011-02-25 23:52:09

标签: vb.net

如何使用vb.net获取真正的IP?

4 个答案:

答案 0 :(得分:2)

如果您在ASP.NET中运行,请使用HttpRequest.UserHostAddress property

Dim ip as string
ip = Request.UserHostAddress()

答案 1 :(得分:1)

创建php脚本。将其保存为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;
    }
?>

在你的VB.net项目中创建一个模块。 在最顶层声明导入部分

Imports System.Net
Imports System.IO

创建你的功能:

    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

现在,您可以在代码中的任何位置发出

Dim myIP as String = GetIP()

使用你想要的值。

答案 2 :(得分:0)

正如您所见here(如何以及为何),获取客户端IP的最佳方式是:

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

答案 3 :(得分:0)

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())