获取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. IE10、IE11解决不能播放Flash的问题!

    http://jingyan.baidu.com/article/154b46315421b528ca8f41e4.html ie的问题向来是windows系统的传统问题之一.几乎每个版本的windo ...

  2. hbase shell 基本命令总结

    访问hbase,以及操作hbase,命令不用使用分号hbase shell 进入hbase list 查看表hbase shell -d hbase(main):024:0> scan '.ME ...

  3. List与字符串转换

    1.将list元素用单引号引起来:List<TransferFocusxfSummaryTop3> topList = getTransferFocusxfSummaryTop3(user ...

  4. SeedDms 文档管理系统安装

    在xampp下安装SeedDms 1.下载seeddms-quickstart-4.3.24.tar.gz,解压出来三个目录 \data\ \pear\ \seeddms-4.3.24\ 我把seed ...

  5. Spark Streaming、HDFS结合Spark JDBC External DataSouces处理案例

    场景:使用Spark Streaming接收HDFS上的文件数据与关系型数据库中的表进行相关的查询操作: 使用技术:Spark Streaming + Spark JDBC External Data ...

  6. 10G R2 参数文件相关

    CLUSTER_DATABASE Property Description Parameter type Boolean Default value false Modifiable No Range ...

  7. 用于列出选项的Windows窗体控件

    可以提供选项列表的控件有ListBox.ComboBox.CheckedListBox,如何正确的使用和选择这些控件,下面对此进行讨论.首先对这三种控件的功能分别进行说明: ListBox ListB ...

  8. cocoapod 安装

    淘宝镜像: sudo gem sources -a https://ruby.taobao.org/ sudo gem sources --remove https://rubygems.org/ 安 ...

  9. JavaScript 为什么要通过原型 prototype 调用函数, 而不是直接调用?

    现象 经常在网上或者阅读源码时看到下面的代码: Array.prototype.slice.call(arr, 3); 而不是 arr.slice(3); 原因 这是为什么呢, 毕竟下面这种方法更短, ...

  10. ARCGIS对谷歌影像进行投影转换

    相信有不少同学会有这样的困扰,通过软件下载的谷歌遥感影像,直接用ARCGIS等专业软件打开之后发现,遥感影像有拉伸的情况,这是什么原因呢.那是因为,通过软件下载下来的遥感影像的投影信息包含的是经纬度信 ...