C#获取真实IP地址实现方法
通常来说,大家获取用户IP地址常用的方法是:
string IpAddress = "";
if((HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]!=null
&& HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] !=String.Empty) )
{
IpAddress=HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ;
}
else
{
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
事实上,上面的代码只试用与用户只使用了1层代理,如果用户有2层,3层HTTP_X_FORWARDED_FOR 的值是:"本机真实IP,1层代理IP,2层代理IP,....." ,如果这个时候你的数据中保存IP字段的长度很小(15个字节),数据库就报错了。
实际应用中,因为使用多层透明代理的情况比较少,所以这种用户并不多。
获取用户真实IP的方法:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
namespace Common
{
/// <summary>
/// IPAddress 的摘要说明
/// </summary>
public class IPAddress : System.Web.UI.Page
{
public static Int64 toDenaryIp ( string ip )
{
Int64 _Int64 = ;
string _ip = ip;
if ( _ip.LastIndexOf ( "." ) > - )
{
string[] _iparray = _ip.Split ( '.' );
_Int64 = Int64.Parse ( _iparray.GetValue ( ).ToString() ) * * * + Int64.Parse ( _iparray.GetValue ( ).ToString() ) * * + Int64.Parse ( _iparray.GetValue ( ).ToString() ) * + Int64.Parse ( _iparray.GetValue ( ).ToString() ) - ;
}
return _Int64;
}
/// <summary>
/// /ip十进制
/// </summary>
public static Int64 DenaryIp
{
get {
Int64 _Int64 = ;
string _ip = IP;
if ( _ip.LastIndexOf ( "." ) > - )
{
string[] _iparray= _ip.Split ( '.' );
_Int64 = Int64.Parse ( _iparray.GetValue ( ).ToString() ) * * * + Int64.Parse ( _iparray.GetValue ( ).ToString() ) * * + Int64.Parse ( _iparray.GetValue ( ).ToString() ) * + Int64.Parse ( _iparray.GetValue ( ).ToString() )-;
}
return _Int64;
}
}
public static string IP
{
get
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if ( result != null && result != String.Empty )
{
//可能有代理
if ( result.IndexOf ( "." ) == - ) //没有"."肯定是非IPv4格式
result = null;
else
{
if ( result.IndexOf ( "," ) != - )
{
//有",",估计多个代理。取第一个不是内网的IP。
result = result.Replace ( " ", "" ).Replace ( "", "" );
string[] temparyip = result.Split ( ",;".ToCharArray() );
for ( int i = ; i < temparyip.Length; i++ )
{
if ( IsIPAddress ( temparyip[i] )
&& temparyip[i].Substring ( , ) != "10."
&& temparyip[i].Substring ( , ) != "192.168"
&& temparyip[i].Substring ( , ) != "172.16." )
{
return temparyip[i]; //找到不是内网的地址
}
}
}
else if ( IsIPAddress ( result ) ) //代理即是IP格式
return result;
else
result = null; //代理中的内容 非IP,取IP
}
}
string IpAddress = ( HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty ) HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; if ( null == result || result == String.Empty )
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if ( result == null || result == String.Empty )
result = HttpContext.Current.Request.UserHostAddress;
return result;
}
}
//是否ip格式
public static bool IsIPAddress ( string str1 )
{
if ( str1 == null || str1 == string.Empty || str1.Length < || str1.Length > ) return false;
string regformat = @"^\\d{1,3}[\\.]\\d{1,3}[\\.]\\d{1,3}[\\.]\\d{1,3}$";
Regex regex = new Regex ( regformat, RegexOptions.IgnoreCase );
return regex.IsMatch ( str1 );
}
}
}
C#获取真实IP地址实现方法的更多相关文章
- JSP 获取真实IP地址的代码
[转载]JSP 获取真实IP地址的代码 JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的. ...
- 【转载】JSP 获取真实IP地址的代码
JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的. 但是在通过了 Apache,Squid ...
- 查找“CDN、负载均衡、反向代理”等大型网络真实IP地址的方法
首先,CDN.负载均衡.反向代理还分为很多层,有时查出来的是最外层的 CDN 服务器群,真实的机器是不对外开放的,类似这样的: 用户 → CDN 网络 → 一台或多台真实机器 ↗ CDN Server ...
- Flask框架获取用户IP地址的方法
本文实例讲述了python使用Flask框架获取用户IP地址的方法.分享给大家供大家参考.具体如下: 下面的代码包含了html页面和python代码,非常详细,如果你正使用Flask,也可以学习一下最 ...
- 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址;
package com.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.htt ...
- java 获取真实ip地址
/** * 获取真实ip地址 * @param request * @return */ public static String getIpAddress(HttpServletRequest re ...
- java 客户端获取真实ip地址
在开发工作中,我们常常需要获取客户端的IP.一般获取客户端的IP地址的方法是:request.getRemoteAddr();但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实 ...
- C#如何获取真实IP地址
大家获取用户IP地址常用的方法是 C# 代码 复制 string IpAddress = ""; if((HttpContext.Current.Request.Serve ...
- php获取真实ip地址(转)
REMOTE_ADDR只能获取访问者本地连接中设置的IP,如中南民族大学校园网中自己设置的10.X.XXX.XXX系列IP,而这个函数获取的是局域网网关出口的IP地址, 如果访问者使用代理服务器,将不 ...
随机推荐
- iOS 宏条件编译
在工程的 build settings 中的 preprocessor macros 下, debug: DEBUG=1 TestApi=1 #if !defined (TestApi) config ...
- your local repository contains non-ascii
安装CCS时候遇到 your local repository contains non-ascii 问题. 解决方法: 不要在中文目录下安装.
- IOS设备 UIDevice 获取操作系统 版本 电量 临近手机触发消息检测 (真机亲测可用)
- (void)viewDidLoad { [super viewDidLoad]; // 操作系统 NSString * osName =[[UIDevice currentDevice]syste ...
- 【JavaScript忍者秘籍】
- Iso-Seq学习
SMRT portal安装教程: http://www.pacb.com/wp-content/uploads/2015/09/SMRT-Analysis-Software-Installation- ...
- Lae程序员小漫画(三),仅供一乐
Lae软件开发,快乐程序员!
- java常用IO流数据流小结
类名 常用方法 说明 输入流 InputStream int read(); 只能读字节流,虽然返回值是int,但只有低8位起作用. DataInputStream Type readType() ...
- 2016 、12 、11<本周>
翻了翻记录 想把上周没搞出来的1159和day2T2搞出来.
- UART
一.协议部分: 协议部分转自:http://www.s8052.com/index.htm 串行通信的传送方向通常有三种: 1.为单工,只允许数据向一个方向传送: 2.半双工,允许数据向两个方向中的任 ...
- dependency of static library
一直以来都有一个误区,认为静态库就一定是不含任何依赖的,动态库是含的.这个印象是因为在我们程序中,包含静态库的地方,往往Build好之后直接就可以用,而含DLL的地方,则需要在build好之后的EXE ...