Read the global IP Access user region

Source: Internet
Author: User

This is explained in the first place. IP Library is QQ pure IP library dat file type

  public static string Qqippath = AppDomain.CurrentDomain.BaseDirectory + "\\ipdata\\qqwry.dat";///qq pure IP Library address public            static string Getcity (String IP) {string ipfilepath = Qqippath;            Ipsearch ipsearch = new Ipsearch (Ipfilepath);            Ipsearch.iplocation loc = Ipsearch.getiplocation (IP);        return loc.country;        } public class Ipsearch {FileStream ipfile;        Long IP;        String Ipfilepath; <summary>///constructor///</summary>///<param name= "Ipfilepath" > Pure IP database path </param        > Public Ipsearch (String ipfilepath) {This.ipfilepath = Ipfilepath;            }///<summary>///geography, including country and region///</summary> public struct Iplocation {        public string country, area; }///<summary>///Get the location of the specified IP///</summary>///<param name= "StrIP" > IP to be queried Site </param>        <returns></returns> Public iplocation getiplocation (string strIP) {IP = I            Ptolong (StrIP);            ipfile = new FileStream (Ipfilepath, FileMode.Open, FileAccess.Read);            long[] Iparray = Blocktoarray (Readipblock ());            Long offset = searchip (iparray, 0, iparray.length-1) * 7 + 4; Ipfile.position + = offset;//Skip start IP ipfile.position = READLONGX (3) + 4;//skip End IP iplocation loc = new            Iplocation (); int flag = Ipfile.readbyte ();//Read flag if (flag = = 1)//indicates country and region being diverted {ipfile.position = Re                ADLONGX (3);            Flag = Ipfile.readbyte ();//re-read flag} long countryoffset = Ipfile.position;            Loc.country = ReadString (flag);            if (flag = = 2) {ipfile.position = Countryoffset + 3;            } flag = Ipfile.readbyte ();            Loc.area = ReadString (flag); Ipfile.close ();            Ipfile = null;        Return LOC; }///<summary>///Convert IP in string form to bit long///</summary>///<param name= "StrIP" >&lt  ;/param>///<returns></returns> Public Long Iptolong (string StrIP) {byte[]            Ip_bytes = new Byte[8];            string[] Strarr = Strip.split (new char[] {'. '});                    if (strarr.length-1 >= 3) {for (int i = 0; i < 4; i++) { Ip_bytes[i] = byte.                Parse (Strarr[3-i]);        }} return Bitconverter.toint64 (ip_bytes, 0); }///<summary>///Convert the starting IP in the index block byte blocks to a long array///</summary>///<param name= "IPblock "></param> long[] Blocktoarray (byte[] ipblock) {long[] Iparray = new Long[ipblock.lengt            H/7];            int ipindex = 0;            byte[] temp = new BYTE[8]; for (int i = 0; i < ipblock.length;                i + = 7) {array.copy (ipblock, I, temp, 0, 4);                Iparray[ipindex] = Bitconverter.toint64 (temp, 0);            ipindex++;        } return Iparray; }///<summary>///search for the specified IP from the IP array and return its index///</summary>///<param name= "Iparray" IP array </param>///<param name= "Start" > Specify the starting position of the search </param>///<param name= "End" > Specify the end bit of the search             Place </param>///<returns></returns> int searchip (long[] iparray, int start, int end) {            int middle = (start + end)/2;            if (middle = = start) return middle;            else if (IP < iparray[middle]) return searchip (Iparray, start, middle);        else return searchip (Iparray, middle, end); }///<summary>///Read the index chunk in the IP file///</summary>///<returns></returns> byte[] Readipblock () {Long startposition = READLONGX (4);            Long endposition = READLONGX (4);            Long Count = (endposition-startposition)/7 + 1;//Total number of records ipfile.position = StartPosition;            byte[] IPblock = new Byte[count * 7];            Ipfile.read (ipblock, 0, ipblock.length);            Ipfile.position = startposition;        return ipblock; }///<summary>///Read the specified byte from the IP file and convert bit long///</summary>///<param name= "Bytescoun        T "> number of bytes needed to convert, idea not to exceed 8 bytes </param>///<returns></returns> long readlongx (int bytescount)            {byte[] _bytes = new Byte[8];            Ipfile.read (_bytes, 0, Bytescount);        Return Bitconverter.toint64 (_bytes, 0); }///<summary>///read string from IP file///</summary>///<param name= "flag" > Turn flag </        Param>///<returns></returns>string ReadString (int flag) {if (flag = = 1 | | flag = = 2)//turn sign ipfile.position = Readlon            GX (3);            else ipfile.position-= 1;            list<byte> list = new list<byte> ();            byte B = (byte) ipfile.readbyte (); while (b > 0) {list.                ADD (b);            b = (byte) ipfile.readbyte (); } return Encoding.Default.GetString (list.        ToArray ());            } public string Getuserip () {string IP;            BOOL Iserr = false; if (system.web.httpcontext.current.request.servervariables["http_x_forwarded_for"] = = null) IP = System.Web . httpcontext.current.request.servervariables["REMOTE_ADDR"].            ToString (); else IP = system.web.httpcontext.current.request.servervariables["Http_x_forwarded_for"].            ToString (); if (IP. Length >) {if (IP.       Contains (","))         {IP = IP.                    Split (', ') [0];                Iserr = IsError (IP);                } else {iserr = true;            }} else {iserr = IsError (IP);            } if (Iserr) return "1.1.1.1";        else return IP;        }///<summary>//IP filter///</summary>//<param name= "IP" ></param>            <returns></returns> protected bool IsError (string IP) {string[] temp;            BOOL Iserr = false; temp = IP.            Split ('. '); if (temp. Length = = 4) {for (int i = 0; i < temp. Length; i++) {if (Temp[i].                Length > 3) Iserr = true;            }} else Iserr = true;        return iserr; } public STring getipaddress () {string result = String.Empty;            result = httpcontext.current.request.servervariables["http_x_forwarded_for"]; if (result! = NULL && result! = String.Empty) {//may have an agent if (result.i Ndexof (".") = =-1)//No "."                Must be a non-IPv4 format result = "1.1.1.1"; else {if (result. IndexOf (",")! =-1) {//have ",", estimate multiple proxies.                             Take the first IP that is not an intranet. result = result. Replace ("", "").                        Replace ("\" "," "); string[] Temparyip = result. Split (",;".                        ToCharArray ()); for (int i = 0; i < Temparyip. Length; i++) {if (! IsError (Temparyip[i]) && Temparyip[i].                                Substring (0, 3)! = "10." && Temparyip[i]. Substring (0, 7)! = "192.1"&& Temparyip[i].                            Substring (0, 7)! = "172.16.")    {return temparyip[i]; Locate the address that is not an intranet}}} else if (!i                    Serror (Result))//proxy is the IP format return result;   else result = "1.1.1.1"; Content in agent non-IP, Fetch IP}} string IpAddress = (HttpContext.Current.Request.ServerVaria bles["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; }    }

Read the global IP Access user region

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.