获取IP  解决request.UserHostAddress取不到真实IP

private string GetClientIP()
  {
   string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
   if (null == result || result == String.Empty)
   {
    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
   }

if (null == result || result == String.Empty)
   {
    result = HttpContext.Current.Request.UserHostAddress;
   }
   return result;
  }

获取MAC地址:

[DllImport("Iphlpapi.dll")] 
  private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length); 
  [DllImport("Ws2_32.dll")]
  private static extern Int32 inet_addr(string ip);

private void Page_Load(object sender, System.EventArgs e)
  {
   try
   {
    string userip=Request.UserHostAddress;
    string strClientIP = Request.UserHostAddress.ToString().Trim();
    Int32 ldest = inet_addr(strClientIP); 
    Int32 lhost = inet_addr("");   
    Int64 macinfo = new Int64(); 
    Int32 len = 6; 
    int res = SendARP(ldest,0, ref macinfo, ref len);
    string mac_src=macinfo.ToString("X");
    if(mac_src == "0")
    {
     if(userip=="127.0.0.1")
      Response.Write ("");
     else
      Response.Write ("" + userip + "" + "<br>");
     return;
    }

while(mac_src.Length<12)
    {
     mac_src = mac_src.Insert(0,"0");
    }

string mac_dest="";

for(int i=0;i<11;i++)
    {
     if (0 == (i % 2))
     {
      if ( i == 10 )
      {
       mac_dest = mac_dest.Insert(0,mac_src.Substring(i,2));
      }
      else
      {
       mac_dest ="-" + mac_dest.Insert(0,mac_src.Substring(i,2));
      }
     } 
    }

}
   catch(Exception err) 
   { 
    Response.Write(err.Message);
   }
  }

参考
参考

.net获取IP和MAC地址的更多相关文章

  1. C#获取IP及MAC地址 方法

    C#获取IP及MAC地址 方法,比较齐全 using System.Net; using System; using System.Management; using System.Runtime.I ...

  2. C语言实现Windows下获取IP和MAC地址。

    C语言实现Windows下获取IP和MAC地址. #include <winsock2.h> #include <stdio.h> #include <stdlib.h& ...

  3. Android -- 获取IP和MAC地址

    通过InetAddress.getLocalHost()得到始终是“127.0.0.1”,要想得到真正的网络ip地址要通过下面的方法: 首先新建一个工程,修改AndroidManifest.xml文件 ...

  4. 获取IP和mac地址

    1.获取IP static string GetLocalIp() { string hostname = Dns.GetHostName();//得到本机名 //IPHostEntry localh ...

  5. js获取IP和MAC地址

    1.IP 百度一下有很多 可以用这个 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> ...

  6. 获取客户机的ip和mac地址

    只获取clientIP package com.ppms.utils; import javax.servlet.http.HttpServletRequest; /** * Created by l ...

  7. 使用ARP获取局域网内设备IP和MAC地址

    根据Arp列表数据,查询本地设备在线状态 使用 arp -a 获得所有内网地址,首先看Mod对象 public struct MacIpPair { public string HostName; p ...

  8. 获取平台所有接口的IP和MAC地址

    我们有时候会有获取网口的IP和MAC地址的需求.可以通过ioctl来获取. #include <sys/ioctl.h>#include <net/if.h>#include ...

  9. 获取本机IP、mac地址、计算机名

    python获取本机IP.mac地址.计算机名 在python中获取ip地址和在php中有很大不同,我们先来看一下python 获得本机MAC地址: >>> import uuid ...

随机推荐

  1. [Hibernate] - Generic Dao

    使用泛型写了一个通用的Hibernate DAO类. GenericDao接口 package com.my.dao; import java.io.Serializable; import java ...

  2. jquery跨域请求jsonp

    服务端PHP代码  header('Content-Type:application/json; charset=utf-8'); $arr = array('a'=>1, 'b'=>2, ...

  3. List和Map之间的转换和关联

    首先,Map.values返回的是此Map中包含的所有值的collection视图. 然后利用ArrayList的构造器ArrayList(Collection<? extends E> ...

  4. Android开发艺术探索读书笔记——01 Activity的生命周期

    http://www.cnblogs.com/csonezp/p/5121142.html 新买了一本书,<Android开发艺术探索>.这本书算是一本进阶书籍,适合有一定安卓开发基础,做 ...

  5. iis 应用程序池看不到 .net framework 4.0

    我的情况是,先配置了iis,然后再安装.net framework 4.0 进去设置应用程序池的时候,没有找到 .net framework 4.0 ,经过一番尝试,无效,最后无奈重启. 好了.

  6. [HTML5]块和内联元素的嵌套

    块元素可以包含块或内联元素,但是内联元素只能包含其他内联元素. <!-- 无效代码! :-( --> <strong> <p>你不应该把p元素放在 "st ...

  7. gc之四--Minor GC、Major GC和Full GC之间的区别

    针对HotSpot VM的实现,它里面的GC其实准确分类只有两大种: Partial GC:并不收集整个GC堆的模式 Young GC:只收集young gen的GC Old GC:只收集old ge ...

  8. vmware workstation LINUX磁盘扩容

    1.edit virtual machine settings -> 选中硬盘->右侧utilities->expand(虚拟机不能存在镜像),输入要扩容到的大小 2.扩容之后进入系 ...

  9. 引用google的jQuery文件

    1. 很多网站都是使用这种方式引入,客户的浏览器可能已经缓存过了 jquery.可以直接调用本地的,速度更快… 2. Google code 使用了 cdn 技术在很多地方有节点服务器,加载 jque ...

  10. 在脚本中刷新impala元信息

    刷新impala元信息 impala-shell -q 'invalidate metadata' -i hslave1 impala-shell -q 'select count(*) from p ...