获取登录的IP或者信息

这是转载的,也不想去检查性能,对于这些成熟的代码,发在这里完全是懒,仅此而已!

1、获取客户端IP

  1. /// <summary>
  2. /// 获取客户端Ip
  3. /// </summary>
  4. /// <returns></returns>
  5. public String GetClientIp()
  6. {
  7. String clientIP = "";
  8. if (System.Web.HttpContext.Current != null)
  9. {
  10. clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  11. if (string.IsNullOrEmpty(clientIP) || (clientIP.ToLower() == "unknown"))
  12. {
  13. clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
  14. if (string.IsNullOrEmpty(clientIP))
  15. {
  16. clientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  17. }
  18. }
  19. else
  20. {
  21. clientIP = clientIP.Split(‘,‘)[0];
  22. }
  23. }
  24. return clientIP;
  25. }

2、服务器端获取客户端请求IP和客户端机器名称

  1. /// <summary>
  2. /// 服务器端获取客户端请求IP和客户端机器名称
  3. /// </summary>
  4. public static void GetClientInfo()
  5. {
  6. OperationContext context = OperationContext.Current;
  7. MessageProperties messageProperties = context.IncomingMessageProperties;
  8. RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
  9. HttpRequestMessageProperty requestProperty = messageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
  10. string clientIp = !string.IsNullOrEmpty(requestProperty.Headers["X-Real-IP"]) ? requestProperty.Headers["X-Real-IP"] : endpointProperty.Address;
  11. string clientName = Environment.MachineName;
  12. Console.WriteLine("ClientIp: " + clientIp + "clientName:" + clientName);
  13. }
时间: 2024-07-31 16:53:10

获取登录的IP或者信息的相关文章

C++获取本机IP等信息

运行环境:VS2008,win7,代码来源于MSDN,相关函数可以查看MSDN中的函数定义.. 代码如下: 1 #include <winsock2.h> 2 #include <ws2tcpip.h> 3 #include <stdio.h> 4 #include <windows.h> 5 #pragma comment(lib, "ws2_32.lib") 6 7 int main(int argc, char **argv) 8

使用PowerShell脚本获取远程主机的IP配置信息

使用PowerShell脚本获取远程主机的IP配置信息 代码如下: [cmdletbinding()]param ([parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][string[]]$ComputerName = $env:computername) begin {}process {foreach ($Computer in $ComputerName) {if(Test-Connection

Linux 获取登录者IP

在linux中有时须要获得登录者的IP,这里有两种方法.先使用who am i 获取登录IP,然后截取字符串: 1.awk截取,sed替换 who am i | awk '{print $5}' | sed 's/(//g' | sed 's/)//g' 2.cut 截取 who am i|cut -d\( -f2|cut -d\) -f1 用法,若在脚本中如.bashrc中,能够使用` `符号来运行脚本.·符号位位于键盘左上角第二排第一个键.比如 export REMOTE_IP=`who a

JS获取登录者IP和登录城市

登录城市:<sp class="cy"></sp><br /> 管理员个数:<font color="#538ec6"><strong>2</strong></font> 人<br /> 登陆者IP:<span class="sp"></span><br /> <script src="http://

java获取登录用户ip地址

public class IPUtil { /** * 获取ip * @param request * @return */ public static String getIp(HttpServletRequest request) { if (request == null) return ""; String ip = request.getHeader("X-Requested-For"); if (StringUtil.isEmpty(ip) || &qu

后台获取登录的IP地址

public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP");

获取登录用户ip

public static String getIpAddr(HttpServletRequest request) { String ipAddress = null; try { ipAddress = request.getHeader("x-forwarded-for"); if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {

asp.net获取URL和IP地址

(转自:http://www.cnblogs.com/JuneZhang/archive/2010/11/26/1888863.html) HttpContext.Current.Request.Url.ToString() 并不可靠.如果当前URL为 http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%BC%BC%CA%F5 通过HttpContext.Current.Request.Url.ToString()获取到

Java服务器获取客户端的ip

原文:http://www.open-open.com/code/view/1454133120089 /** * 获取登录用户IP地址 * * @param request * @return */ public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() ==