Obtain the real Internet IP address, IP address country, province, and region (thieves Program)

Source: Internet
Author: User

This method is not efficient. If it is used in the WEB, you can use this method if it is rarely used. If it is commonly used, it is recommended to use the IP library.
The Code is as follows:
# Region # obtain the real IP address and location details
/// <Summary>
/// Obtain the real IP address and location details (Porschev)
/// </Summary>
/// <Returns> </returns>
Public string GetIpDetails ()
{
String url = "http://www.ip138.com/ips8.asp"; // sets the url for obtaining IP addresses and country source code
String regStr = "(? <= <Td \ s * align =\\ "center \">) [^ <] *? (? = <Br/> </td> )";
String ipRegStr = "(2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) \.) {3} (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) "; // IP address regular
String ip = string. Empty; // ip address
String country = string. Empty; // country
String adr = string. Empty; // province/City
String html = GetHtml (url); // obtain the webpage source code
Regex reg = new Regex (regStr, RegexOptions. None );
Match ma = reg. Match (html); html = ma. Value;
Regex ipReg = new Regex (ipRegStr, RegexOptions. None );
Ma = ipReg. Match (html );
Ip = ma. Value; // obtain the IP address.
Int index = html. LastIndexOf (":") + 1;
Country = html. Substring (index); // obtain the country
Adr = GetAdrByIp (ip );
Return "IP:" + ip + "country:" + country + "province and city:" + adr;
}
# Endregion

# Region # obtain the province and city of the IP address through IP Address
/// <Summary>
/// Obtain the city (Porschev) where the IP address is located through the IP address)
/// </Summary>
/// <Param name = "ip"> </param>
/// <Returns> </returns>
Public string GetAdrByIp (string ip)
{
String url = "http://www.cz88.net/ip? Ip = "+ ip;
String regStr = "(? <= <Span \ s * id = \ "cz_addr \"> ).*? (? = </Span> )";
String html = GetHtml (url); // obtain the webpage source code
Regex reg = new Regex (regStr, RegexOptions. None );
Match ma = reg. Match (html );
Html = ma. Value;
String [] arr = html. Split ('');
Return arr [0];
}
# Endregion

# Region # obtain HTML source code information
/// <Summary>
/// Obtain the HTML source code (Porschev)
/// </Summary>
/// <Param name = "url"> obtain the address </param>
/// <Returns> HTML source code </returns>
Public string GetHtml (string url)
{
Uri uri = new Uri (url );
WebRequest wr = WebRequest. Create (uri );
Stream s = wr. GetResponse (). GetResponseStream ();
StreamReader sr = new StreamReader (s, Encoding. Default );
Return sr. ReadToEnd ();
}
# Endregion

Related Article

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.