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

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. }

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

  1. Java获取登录用户IP地址

    /** * 获取登录用户IP地址 * * @param request * @return */ public static String getIpAddr(HttpServletRequest r ...

  2. Linux 获取登录者IP

    在linux中有时须要获得登录者的IP,这里有两种方法.先使用who am i 获取登录IP,然后截取字符串: 1.awk截取,sed替换 who am i | awk '{print $5}' | ...

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

    登录城市:<sp class="cy"></sp><br /> 管理员个数:<font color=</strong>< ...

  4. C++获取本机IP地址信息

    #include<winsock2.h> #include<iostream> #include<string> using namespace std; #pra ...

  5. 获取登录用户ip

    public static String getIpAddr(HttpServletRequest request) { String ipAddress = null; try { ipAddres ...

  6. C++获取本机IP等信息

    运行环境:VS2008,win7,代码来源于MSDN,相关函数可以查看MSDN中的函数定义.. 代码如下: #include <winsock2.h> #include <ws2tc ...

  7. asp.net获取URL和IP地址

    (转自:http://www.cnblogs.com/JuneZhang/archive/2010/11/26/1888863.html) HttpContext.Current.Request.Ur ...

  8. Java服务器获取客户端的ip

    原文:http://www.open-open.com/code/view/1454133120089 /** * 获取登录用户IP地址 * * @param request * @return */ ...

  9. 使用 python 获取 Linux 的 IP 信息(通过 ifconfig 命令)

    我们可以使用 python 代码通过调用 ifconfig 命令来获取 Linux 主机的 IP 相关信息,包括:网卡名称.MAC地址.IP地址等. 第一种实现方式: #!/usr/bin/pytho ...

随机推荐

  1. 计算两个日期相隔的天数(jodd)

    public static void main(String[] args) throws ParseException { System.out.println(TimeUtil.dayOfYear ...

  2. 对同一元素设置overflow-x:hidden,overflow-y:visible;属性值不生效

    作者:孙志勇 微博 日期:2016年12月5日 一.时效性 所有信息都具有时效性.文章的价值,往往跟时间有很大关联.特别是技术类文章,请注意本文创建时间,如果本文过于久远,请读者酌情考量,莫要浪费时间 ...

  3. PL/pgSQL学习笔记之二

    39.1.1 使用 PL/pgSQL的好处 SQL是 PostgreSQL和其他大多数关系型数据库作为查询语言而使用的语言.它可移植,并容易学习.但是SQL语句必须被数据库服务器逐条地执行. 这意味着 ...

  4. IOS 日期选择

    传统方式 一般情况下弹出日期选择的场景是:用户点击UITextField弹出日期选择,关键代码如下: 点击UITextField弹出日期选择 1 2 3 UITextField *textField; ...

  5. [AngularJS] Directive with Transcluded Elements

    Create a wrapWith directive using advanced transclusion techniques. transclude - compile the content ...

  6. oc-10-对象做参数

    // // main.m // 6-[掌握]对象和方法之间的关系 #import <Foundation/Foundation.h> //声明人类 @interface Person : ...

  7. android152 笔记 4

    42. Android中Task任务栈的分配. 首先我们来看下Task的定义,Google是这样定义Task的:a task is what the user experiences as an &q ...

  8. 1043. Is It a Binary Search Tree (25)

    the problem is from pat,which website is http://pat.zju.edu.cn/contests/pat-a-practise/1043 and the ...

  9. A Simple Actions Recognition System

    1. Problem Definition There's no doubt that researches and applications on the foundation of videos ...

  10. u检验、t检验、F检验、X2检验 (转)

    http://blog.renren.com/share/223170925/14708690013 常用显著性检验 1.t检验 适用于计量资料.正态分布.方差具有齐性的两组间小样本比较.包括配对资料 ...