zoukankan      html  css  js  c++  java
  • .NET中获取IP地址

    在.NET中获取一台电脑名,IP地址及当前用户名是非常简单,以下是我常用的几种方法,如果大家还有其它好的方法,可以回复一起整理:

      1. 在ASP.NET中专用属性:
      获取服务器电脑名:Page.Server.ManchineName
      获取用户信息:Page.User
      获取客户端电脑名:Page.Request.UserHostName
      获取客户端电脑IP:Page.Request.UserHostAddress

      2. 在网络编程中的通用方法:
      获取当前电脑名:static System.Net.Dns.GetHostName()
      根据电脑名取出全部IP地址:static System.Net.Dns.Resolve(电脑名).AddressList
      也可根据IP地址取出电脑名:static System.Net.Dns.Resolve(IP地址).HostName

      3. 系统环境类的通用属性:
      当前电脑名:static System.Environment.MachineName
      当前电脑所属网域:static System.Environment.UserDomainName
      当前电脑用户:static System.Environment.UserName

    Request.UserHostAddress
    string IP   =   (Request.ServerVariables["HTTP_VIA"]!=null)?Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString():Request.ServerVariables["REMOTE_ADDR"].ToString();
    string   ipAddress   =   System.Web.HttpContext.Current.Request.UserHostAddress;

    先引用命名空间:using   System.Net;   
      得到客户端IP地址:Request.ServerVariables["REMOTE_ADDR"]或Rqurest.UserHostAddress();   
      得到本机IP地址:Request.ServerVariables["local_addr"]或addr   =   new   System.Net.IPAddress   (   Dns.GetHostByName   (   Dns.GetHostName   (   )   )   .AddressList   [0].Address   )   ;   

    HttpBrowserCapabilities   bc=   Request.Browser;     
      Label3.Text=bc.Browser+"   "+bc.Version;//得到浏览器版本   
      Label2.Text=bc.Platform;//得到操作系统   
      hostname=Dns.GetHostName();   
      IPHostEntry   jj=Dns.GetHostByName(hostname);//得到IP   
      //IPAddress   ip=Dns.Resolve(hostname);   
      Label1.Text=jj.AddressList[0].ToString();

  • 相关阅读:
    Unique Binary Search Trees——LeetCode
    Binary Tree Inorder Traversal ——LeetCode
    Maximum Product Subarray——LeetCode
    Remove Linked List Elements——LeetCode
    Maximum Subarray——LeetCode
    Validate Binary Search Tree——LeetCode
    Swap Nodes in Pairs——LeetCode
    Find Minimum in Rotated Sorted Array——LeetCode
    Linked List Cycle——LeetCode
    VR AR MR
  • 原文地址:https://www.cnblogs.com/KingsLiu/p/5807810.html
Copyright © 2011-2022 走看看