/// 获得客户端IP
///
///
private string getIp()
{
// 穿过代理服务器取远程用户真实IP地址
string Ip = string.Empty;
if (Request.ServerVariables["HTTP_VIA"] != null)
{
if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
{
if (Request.ServerVariables["HTTP_CLIENT_IP"] != null)
Ip = Request.ServerVariables["HTTP_CLIENT_IP"].ToString();
else
if (Request.ServerVariables["REMOTE_ADDR"] != null)
Ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
else
Ip = "202.96.134.133";
}
else
Ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (Request.ServerVariables["REMOTE_ADDR"] != null)
{
Ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
}
else
{
Ip = "202.96.134.133";
}
return Ip;
}
//控制台应用程序
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string hostName = Dns.GetHostName(); //获取本机主机名
Console.WriteLine("Local hostname: {0}", hostName);
IPHostEntry myself = Dns.GetHostEntry(hostName);//获取本机ip地址信息
foreach (IPAddress address in myself.AddressList)
{
Console.WriteLine("IP Address: {0}", address.ToString());
}
}
}
}