获取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. 函数调用方式__stdcall、__cdel

    函数调用方式关系对比如下: 关键字 调用规则 参数传递方向 返回 参数寄存器 堆栈的清除 __cdecl C语言 从右向左 EAX 无 调用者 __stdcall Win32标准  从右向左 EAX ...

  2. Verilog HDL那些事_建模篇笔记(实验一,实验二)

    实验一:永远的流水灯 扫描频率配置为100Hz,即是说扫描周期为10ms.这里需要注意的是扫描周期的概念.流水灯嘛,顾名思义,扫描周期指的是流水灯扫一轮所需要的时间.听到说周期,就应该想到在建模的时候 ...

  3. swoole和erlang通信测试

    直接用docker跑环境 docker pull xlight/docker-php7-swoole docker run -it -v ~/Projects/php/swoole:/workdir ...

  4. (DP)3.Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. FreeBSD 10 中文环境

    中文字体位置 FontPath        "/usr/local/lib/X11/fonts/misc/:unscaled" FontPath        "/us ...

  6. Linux下的shell编程(一)BY 四喜三顺

    Ctrl + Alt + Tvim文本编辑器:i(插入模式),v(可视模式),Esc(普通模式),:w(保存),:r(打开),:q(退出)gvim 文本编辑器vim的升级版gedit 更常用的文本编辑 ...

  7. COM调用 – VB、PB

    本文使用Delphi和C++创建CRC32的COM文件(Dll). VB: V6.0 PB: V8.0 Delphi创建的文件,提供给VB6调用:C++创建的文件,提供给PB8调用. 一.VB部分 C ...

  8. 用通俗易懂的大白话讲解Map/Reduce原理

    Hadoop简介 Hadoop就是一个实现了Google云计算系统的开源系统,包括并行计算模型Map/Reduce,分布式文件系统HDFS,以及分布式数据库Hbase,同时Hadoop的相关项目也很丰 ...

  9. 执行powershell脚本

    打开powershell运行窗口: powershell.exe C:\Users\Administrator\Desktop\a.ps1 -a $a. -a $a :  vbs脚本路径(如 C:\h ...

  10. ORACLE 常用函数 日期/时间函数

    ---------------------------------------------日期/时间函数----------------------------------------------- ...