RequestMachine

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
 
 
namespace com.Longle.CommonLibrary.Web
{
    public class RequestMachine
    {
        [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);
 
        public RequestMachine()
        {
        }
 
        #region 客户端MAC地址
 
        public string HostMAC()
        {
            //***默认返回IP
            string reMac = System.Web.HttpContext.Current.Request.UserHostAddress;
 
            try
            {
                string userip = System.Web.HttpContext.Current.Request.UserHostAddress;
                string strClientIP = System.Web.HttpContext.Current.Request.UserHostAddress.ToString().Trim();
                Int32 ldest = inet_addr(strClientIP); //目的地的ip
                Int32 lhost = inet_addr("");   //本地服务器的ip
                Int64 macinfo = new Int64();
                Int32 len = 6;
                int res = SendARP(ldest, 0, ref macinfo, ref len);
                string mac_src = macinfo.ToString("X");
                //****本机localhost访问,直接返回
                if (mac_src == "0")
                {
                    //***获取客户端机器名
                    string[] computername = System.Net.Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables["remote_addr"]).HostName.ToString().Split(new Char[] { '.' }); computername[0].ToString();
 
                    string clientName = "";
                    //***存在客户端机器名,获取
                    if (computername.Length > 0)
                    {
                        clientName = computername[0].ToString();
                    }
                    else
                    {
                        clientName = System.Web.HttpContext.Current.Request.UserHostName.ToString();
                    }
 
                    reMac = clientName;
                }
                else
                {//***非localhost访问,获取客户端IP+MAC地址
                    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));
                            }
                        }
                    }
 
                    reMac = mac_dest;
                }
            }
            catch 
            {//****出错,直接返回客户端IP
                reMac = System.Web.HttpContext.Current.Request.UserHostName;
            }
 
            return reMac;
        }
 
        #endregion
 
        #region 客户端IP地址
 
        public string HostIP()
        {
            //***默认返回IP
            string reMac = System.Web.HttpContext.Current.Request.UserHostAddress;
 
            try
            {
                string userip = System.Web.HttpContext.Current.Request.UserHostAddress;
                string strClientIP = System.Web.HttpContext.Current.Request.UserHostAddress.ToString().Trim();
                Int32 ldest = inet_addr(strClientIP); //目的地的ip
                Int32 lhost = inet_addr("");   //本地服务器的ip
                Int64 macinfo = new Int64();
                Int32 len = 6;
                int res = SendARP(ldest, 0, ref macinfo, ref len);
                string mac_src = macinfo.ToString("X");
                //****本机localhost访问,直接返回IP
                if (mac_src == "0")
                {
                    //***获取客户端机器IP
                    reMac = System.Web.HttpContext.Current.Request.UserHostAddress;
                }
                else
                {//***非localhost访问,获取客户端IP
                    reMac = userip;
                }
            }
            catch 
            {//****出错,直接返回客户端IP
                reMac = System.Web.HttpContext.Current.Request.UserHostAddress;
            }
 
            return reMac;
        }
 
        #endregion
 
    }
}
posted @   Bruce-He  阅读(293)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
成都共享办公室
点击右上角即可分享
微信分享提示