2011-02-25 35 views
2

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

+2

没有任何IP地址比其他任何IP地址都“真实”,所以您真正想要的是哪个IP地址? – Guffa 2011-02-25 23:53:14

+1

@Guffa可能127.0.0.1:P但这只是形而上学的猜测 – 2011-02-25 23:54:11

+0

@Federico Culloca:诅咒你!打我吧... – 2011-02-25 23:57:23

回答

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

从那里使用的值你希望。