用Javascript获取客户端IP和Mac,在VS2010中调试,执行到
var locator = new ActiveXObject("WbemScripting.SWbemLocator");
时,报错:Microsoft JScript 运行时错误: Automation 服务器不能创建对象,
求解,代码如下:
<script id="clientEventHandlersJS" type="text/javascript" language="javascript">
function GetLocalNetInfo() {
var locator =new ActiveXObject("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
var e =new Enumerator(properties);
for (; !e.atEnd(); e.moveNext()) {
var p = e.item();
if (p.IPAddress==null) {
continue;
}
document.write(p.IPAddress(0));
document.write(p.MACAddress);
document.write("<br/>");
}
}
GetLocalNetInfo()
</script>
网上找的基本上都是这个办法,抄来抄去的,各位还有没有其他办法?
active插件,只能用于ie吧?这个没什么意义。。。
这个有么什么意义.
这个就可以获得,不过只适用于IE
http://q.cnblogs.com/q/15854/
运行结果:
楼主,给你一个忠告,这是无法实现的.因为.NET在服务器运行时,这个页面是不容许使用的,我也做过.
是不是要注册一下,或者ie没有设置安全等级之类的
通过.net实现
代码如下:
public static string GetIP()
{
string HostName = Dns.GetHostName(); //get Hostname
IPHostEntry IpEntry = Dns.GetHostEntry(HostName); //get IP
string strIPAddr = IpEntry.AddressList[0].ToString();
return (strIPAddr);
}
/// <summary>
/// get MacAddress
/// </summary>
/// <returns></returns>
public static string GetMac()
{
string MacAddress = String.Empty;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
MacAddress = mo["MacAddress"].ToString();
break;
}
}
moc = null;
mc = null;
MacAddress = MacAddress.Replace(":", "");
return MacAddress;
}
你这个是服务器的吧
@DanyGao: 是的
学习了
还是走后台吧