C # obtain city information based on ip Address

Source: Internet
Author: User

Copy codeThe Code is as follows: // <summary>
/// Obtain the real IP address and location details (Porschev)
/// </Summary>
/// <Returns> </returns>
Public string GetIpDetails ()
{
// Set the URL for obtaining IP addresses and country source code
String url = "http://www.ip138.com/ips8.asp ";
String regStr = "(? <= <Td \ s * align =\\ "center \">) [^ <] *? (? = <Br/> </td> )";
// IP address regular
String ipRegStr = "(2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) \.) {3} (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) ";
// Ip address
String ip = string. Empty;
// Country
String country = string. Empty;
// Province/City
String adr = string. Empty;
// Obtain the webpage source code
String html = GetHtml (url );
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 );
// Obtain the IP address
Ip = ma. Value;
Int index = html. LastIndexOf (":") + 1;
// Obtain the country
Country = html. Substring (index );
Adr = GetAdrByIp (ip );
Return "IP:" + ip + "country:" + country + "province and city:" + adr;
}
/// <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> )";
// Obtain the webpage source code
String html = GetHtml (url );
Regex reg = new Regex (regStr, RegexOptions. None );
Match ma = reg. Match (html );
Html = ma. Value;
String [] arr = html. Split ('');
Return arr [0];
}

Copy codeThe Code is as follows: // <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)
{
String str = "";
Try
{
Uri uri = new Uri (url );
WebRequest wr = WebRequest. Create (uri );
Stream s = wr. GetResponse (). GetResponseStream ();
StreamReader sr = new StreamReader (s, Encoding. Default );
Str = sr. ReadToEnd ();
}
Catch (Exception e)
{
}
Return str;
}

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.