获取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. C#特性学习笔记一

    元数据,就是C#中封装的一些类,无法修改.类成员的特性被称为元数据中的注释. 1.什么是特性   1)属性与特性的区别 属性(Property):属性是面向对象思想里所说的封装在类里面的数据字段,Ge ...

  2. C#:org.in2bits.MyXls 文本格式日期 转换,以及设置单元格格式,保留两位小数点

    org.in2bits.MyXls  Excel导入日期格式的处理 表格内容为 2014-7-22 ,导入后显示为 41842 等于一个数值,根本不是日期,后来百度了一下,发现要做如下处理: stri ...

  3. 在AD转换中的过采样和噪声形成

    1. 直接量化的过采样AD转换 此类系统的模型可以用下图表示. 图中xa(t)是输入信号,e(t)是量化引入的噪声,xd[n]是最终得到的数字信号,包含分量xda和xde. 对于M倍过采样,信号与量化 ...

  4. Spring AOP 简单理解

    AOP技术即(面向切面编程)技术是在面向对象编程基础上的发展,AOP技术是对所有对象或一类对象编程.核心是在不增加代码的基础上,还增加了新的功能.AOP编程在开发框架本身用的比较多,而实际项目中,用的 ...

  5. oracle远程连接太慢

    场景:Oracle11G DB服务器上用sqlplus直接连很快(秒连),但加上服务名后却半天没反应.一台正式.一台测试都出现此问题. sqlplus:xnwz/xnwz 很快 sqlplus:xnw ...

  6. (C# Binary Tree) 基本概念和算法

    A binary tree is defined as a tree where each node can have no more than two children. Building a Bi ...

  7. Yii里增删改查的操作方法

    一.AR $model=New user();//user是数据库中的一张表,有id,name,pwd字段 1.增加: <1. $model->name='张三': $model-> ...

  8. 树莓派 自身摄像头的opencv调用

    之前写过一篇随笔关于树莓派3上摄像头的调用,使用的方式是安装v4l2驱动. 实际上有一种更加简单的方法. 树莓派中的camera module是放在/boot/目录下以固件的形式加载的,不是一个标准的 ...

  9. iOS 设置页面的代码编写

    突然觉得好久没有更新博客了,今天就想把自己的项目中的一些功能和常用的模块写出来给大家参考一下... 这是我的二个项目中的不同的设置界面,第一个设置的那个按钮是 用的开关switch ,当然这个就容易一 ...

  10. 10.11 安装pod

    原文地址:http://www.jianshu.com/p/5fc15906c53a  感谢. 更新升级10.11 cocoapods安装出问题最简单的解决方法 这是因为10.11把cocoapods ...