zoukankan      html  css  js  c++  java
  • ASP.NET获取IP的6种方法 ( 转)

    原文转自:http://www.cnblogs.com/blodfox777/archive/2008/07/21/1247447.html

    服务端:

     1 //方法一
     2 
     3 HttpContext.Current.Request.UserHostAddress; 
     4 
     5 //方法二
     6 HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
     7 
     8 //方法三
     9 string strHostName = System.Net.Dns.GetHostName();
    10 
    11 string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
    12 
    13 //方法四(无视代理)
    14 HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    客户端:

     1 //方法五
     2 var ip = '<!--#echo var="REMOTE_ADDR"-->';
     3 alert("Your IP address is "+ip);
     4 
     5 
     6 //方法六(无视代理)
     7 function GetLocalIPAddress() 
     8 { 
     9     var obj = null; 
    10     var rslt = ""; 
    11     try 
    12     { 
    13         obj = new ActiveXObject("rcbdyctl.Setting"); 
    14         rslt = obj.GetIPAddress; 
    15         obj = null; 
    16     } 
    17 
    18     catch(e) 
    19     { 
    20     } 
    21 
    22     return rslt; 
    23  }

    另一种服务端的解决方案:

    1 if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // using proxy
    2 {    ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.
    3 }
    4 else// not using proxy or can't get the Client IP
    5 { 
    6     ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
    7 }

    备注:

    1. 有些代理是不会发给我们真实IP地址的

    2. 有些客户端会因为“header_access deny”的安全设置而不发给我们IP

  • 相关阅读:
    recyclerView DiffUtil使用
    RecyclerView实现侧滑删除、置顶、滑动
    Android RecyclerView
    视频框架 Vitamio使用
    react native初始化项目
    react native环境搭建
    javascript Promise
    javascript 基础
    [IOS学习笔记]KVO
    [IOS学习笔记] UINavigationController Demo
  • 原文地址:https://www.cnblogs.com/xiaoerlang90/p/5473883.html
Copyright © 2011-2022 走看看