using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Net ;
using System. Text ;
using System. Threading. Tasks ;
using System. Web ;
namespace ConsoleApp1
{
class Program
{
private static void Main ( string [ ] args)
{
string msg = GetHostStr ( null ) ;
System. Console. WriteLine ( "IP:" + msg) ;
Console. ReadKey ( ) ;
}
public static string GetHostStr ( HttpRequestBase request)
{
var ipList = new List< IPAddress> ( ) ;
var addressList = Dns. GetHostEntry ( Dns. GetHostName ( ) ) . AddressList;
foreach ( var add in addressList)
{
try
{
long a = add . ScopeId;
}
catch
{
ipList. Add ( add ) ;
}
}
var host = ipList. Count > 0 ? ipList[ 0 ] . ToString ( ) : request. Url. Host;
return host;
}
}
}
public static string ClientIP
{
get
{
var context = HttpContext. Current;
string result = "" ;
if ( context== null || context. Request== null )
{
result = "localHost" ;
}
if ( string . IsNullOrEmpty ( result) )
{
result = context. Request. ServerVariables[ "HTTP_X_FORWARDED_FOR" ] ;
}
if ( string . IsNullOrEmpty ( result) )
{
result = context. Request. ServerVariables[ "REMOTE_ADDR" ] ;
}
if ( string . IsNullOrEmpty ( result) )
{
result = context. Request. UserHostAddress;
}
return result;
}
}