zoukankan      html  css  js  c++  java
  • C# 之 获取服务器IP,客户端IP以及其它

    1、获取客户端IP:Request.ServerVariables.Get("Remote_Addr").ToString();

    2、获取客户端主机名:Request.ServerVariables.Get("Remote_Host").ToString();

    3、客户端浏览器:Request.Browser.Browser;

    4、客户端浏览器 版本号:Request.Browser.MajorVersion;

    5、客户端操作系统:Request.Browser.Platform;

    6、获取服务器IP:Request.ServerVariables.Get("Local_Addr").ToString();

    7、获取服务器名:Request.ServerVariables.Get("Server_Name").ToString();


    如果你想进一步了解ServerVariables,可以用

    foreach(String o in Request.ServerVariables){
        Response.Write(o+"="+Request.ServerVariables[o]+"<br>");
    }
    string stringMAC = "";
       string stringIP = "";
       ManagementClass MC = new ManagementClass ("Win32_NetworkAdapterConfiguration");
       ManagementObjectCollection MOC= MC.GetInstances();
    
       foreach(ManagementObject MO in MOC)
       {
        if ((bool)MO["IPEnabled"] == true)
        {
           stringMAC += MO["MACAddress"].ToString(); //获取网卡的地址
           string[] IPAddresses = (string[]) MO["IPAddress"]; //获取本地的IP地址
           if(IPAddresses.Length > 0)
           stringIP = IPAddresses[0];
           Response.Write(stringMAC+"/"+stringIP);
         }
       }



    asp.net
    +c#如何获取客户端网卡的MAC地址?

    //要引用到以下两个命名空间
    using System.Diagnostics;
    using System.Text.RegularExpressions;
    
    //获取远程客户端的网卡物理地址(MAC)
    public string GetMac(string IP)   //para IP is the client's IP
    {
    string dirResults="";
    ProcessStartInfo psi = new ProcessStartInfo();
    Process proc = new Process();
    psi.FileName = "nbtstat";
    psi.RedirectStandardInput = false;
    psi.RedirectStandardOutput = true;
    psi.Arguments = "-A " + IP;
    psi.UseShellExecute = false;
    proc = Process.Start(psi);
    dirResults = proc.StandardOutput.ReadToEnd();
    proc.WaitForExit();
    dirResults=dirResults.Replace("
    ","").Replace("
    ","").Replace("	","");
    
    Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled);
    Match mc=reg.Match(dirResults+"__MAC");
    
    if(mc.Success)   
    {
    return mc.Groups["key"].Value;
    }
    else
    {
    reg=new Regex("Host not found",RegexOptions.IgnoreCase|RegexOptions.Compiled);
    mc=reg.Match(dirResults);
    if(mc.Success)
    {
    return "Host not found!";
    }
    else
    {
    return "";
    }
    }
    }
    
    //在页面上打印出客户端的网卡物理地址(MAC)
    Response.Write(this.GetMac(Request.UserHostAddress.ToString()));



     获取cpu序列号,硬盘ID,网卡MAC地址
    private void GetInfo()
      
    {
       
    string cpuInfo = "";//cpu序列号
       ManagementClass cimobject = new ManagementClass("Win32_Processor");
       ManagementObjectCollection moc 
    = cimobject.GetInstances();
       
    foreach(ManagementObject mo in moc)
       
    {
        cpuInfo 
    = mo.Properties["ProcessorId"].Value.ToString();
        Response.Write (
    "cpu序列号:"+cpuInfo.ToString ());
       }


       
    //获取硬盘ID
       String HDid;
       ManagementClass cimobject1 
    = new ManagementClass("Win32_DiskDrive");
       ManagementObjectCollection moc1 
    = cimobject1.GetInstances();
       
    foreach(ManagementObject mo in moc1)
       
    {
        HDid 
    = (string)mo.Properties["Model"].Value;
        Response.Write (
    "硬盘序列号:"+HDid.ToString ());
       }



       
    //获取网卡硬件地址

       ManagementClass mc 
    = new ManagementClass("Win32_NetworkAdapterConfiguration");
       ManagementObjectCollection moc2 
    = mc.GetInstances();
       
    foreach(ManagementObject mo in moc2)
       
    {
        
    if((bool)mo["IPEnabled"== true)
         Response.Write(
    "MAC address {0}"+mo["MacAddress"].ToString());
        mo.Dispose();
       }

      

  • 相关阅读:
    angular 封装http请求、解决跨域问题
    TCP协议数据读取
    Dart里继承extends和实现implements的区别
    MySQL通过frm和ibd恢复表结构和数据的办法【基于Linux和MySQL5.6.23测试】
    Dart里实现可增长List和定长List的runtimeType的toString()的值是一样的功能
    MySQL位判断
    SpringBoot URL参数解析坑
    android 实用抓取手机log方法(均为转载)
    python 创建动态类
    Mac技巧之苹果电脑上将一个软件进程的 CPU 占用率限制在指定范围内:cputhrottle
  • 原文地址:https://www.cnblogs.com/xinaixia/p/4772955.html
Copyright © 2011-2022 走看看