C#取真实IP地址--多个代理背后的ip地址

简介:

C#取真实IP地址--多个代理背后的ip地址

//多个代理背后的ip地址

文件名:IPAddress.cs

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 = 0;
        string _ip = ip;
        if ( _ip.LastIndexOf ( "." ) > -1 )
        {
            string[] _iparray = _ip.Split ( '.' );

            _Int64 = Int64.Parse ( _iparray.GetValue ( 0 ).ToString() ) * 256 * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 1 ).ToString() ) * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 2 ).ToString() ) * 256 + Int64.Parse ( _iparray.GetValue ( 3 ).ToString() ) - 1;
        }
        return _Int64;
    }

    /// <summary>
    /// /ip十进制
    /// </summary>
    public static Int64 DenaryIp
    {
        get {
            Int64 _Int64 = 0;

            string _ip = IP;
            if ( _ip.LastIndexOf ( "." ) > -1 )
            {
                string[] _iparray= _ip.Split ( '.' );

                _Int64 = Int64.Parse ( _iparray.GetValue ( 0 ).ToString() ) * 256 * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 1 ).ToString() ) * 256 * 256 + Int64.Parse ( _iparray.GetValue ( 2 ).ToString() ) * 256 + Int64.Parse ( _iparray.GetValue ( 3 ).ToString() )-1;
            }
            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 ( "." ) == -1 ) //没有"."肯定是非IPv4格式
                    result = null;
                else
                {
                    if ( result.IndexOf ( "," ) != -1 )
                    {
                         //有",",估计多个代理。取第一个不是内网的IP。
                        result = result.Replace ( " ", "" ).Replace ( "", "" );
                        string[] temparyip = result.Split ( ",;".ToCharArray() );
                        for ( int i = 0; i < temparyip.Length; i++ )
                        {
                            if ( IsIPAddress ( temparyip[i] )
                                    && temparyip[i].Substring ( 0, 3 ) != "10."
                                    && temparyip[i].Substring ( 0, 7 ) != "192.168"
                                    && temparyip[i].Substring ( 0, 7 ) != "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 < 7 || str1.Length > 15 ) 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 );
    }


}
}
,如需转载请自行联系原作者


相关文章
|
Linux C# Android开发
C#爬虫使用代理刷csdn文章浏览量
昨天写了一篇关于“c#批量抓取免费代理并验证有效性”的文章,接着昨天的目标继续完成吧,最终实现的目的就是刷新csdn文章的浏览量(实际上很简单,之前博客园的文章也是可以使用代理ip来刷的,后来不行了),刷文章的浏览量本身是可耻的,没有任何意义,当然技术无罪。
1599 0
|
NoSQL C# Redis
c#批量抓取免费代理并验证有效性
之前看到某公司的官网的文章的浏览量刷新一次网页就会增加一次,给人的感觉不太好,一个公司的官网给人如此直白的漏洞,我批量发起请求的时候发现页面打开都报错,100多人的公司的官网文章刷新一次你给我看这个,这公司以前来过我们学校宣传招人+在园子里搜招聘的时候发现居然以前招xamarin,挺好奇的,所以就关注过。
1413 0
|
C#
C# WebBrowser 代理的使用
原文:C# WebBrowser 代理的使用   The WebBrowser control is just an embeddded IE Control, I believe any settings in IE, like the proxy settings, are honered just the same as they are in IE.
918 0
|
1月前
|
C#
24. C# 编程:用户设定敌人初始血值的实现
24. C# 编程:用户设定敌人初始血值的实现
22 0
|
2月前
|
SQL 数据库连接 应用服务中间件
C#WinForm基础编程(三)
C#WinForm基础编程
77 0
|
2月前
C#WinForm基础编程(二)
C#WinForm基础编程
58 0
|
2月前
|
C# 数据安全/隐私保护
C#WinForm基础编程(一)
C#WinForm基础编程
62 0
|
4月前
|
数据采集 前端开发 C#
C#编程艺术:Fizzler库助您高效爬取www.twitter.com音频
Twitter是全球最大的社交媒体平台之一,包含丰富的音频资源。用户可以在Twitter上发布、转发、评论和收听各种音频内容,如音乐、播客、新闻、故事等,直接从Twitter抓取音频数据并非易事,尤其是在考虑到可能的封锁和反爬虫机制。Twitter会对频繁访问的IP地址进行限制或封禁,以防止恶意爬虫的行为。因此,我们需要使用一些技术手段来规避这些障碍,确保稳定而高效的数据访问。
C#编程艺术:Fizzler库助您高效爬取www.twitter.com音频