C according to ip to get the city and other relevant information

  • 2020-05-24 05:29:56
  • OfStack

 
/// <summary> 
///  Get real IP And location details ( Porschev )  
/// </summary> 
/// <returns></returns> 
public string GetIpDetails() 
{ 
// Set up access IP Address and country source url  
string url = "http://www.ip138.com/ips8.asp"; 
string regStr = "(?<=<td\\s*align=\\\"center\\\">)[^<]*?(?=<br/><br/></td>)"; 
//IP regular  
string ipRegStr = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)"; 
//IP address  
string ip = string.Empty; 
// countries  
string country = string.Empty; 
// Provinces and cities  
string adr = string.Empty; 
// Get the source  
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); 
// get IP 
ip = ma.Value; 
int index = html.LastIndexOf(" : ") + 1; 
// By the national  
country = html.Substring(index); 
adr = GetAdrByIp(ip); 
return "IP : " + ip + "  Countries: " + country + "  Provinces and cities: " + adr; 
} 
/// <summary> 
///  through IP get IP Province ( Porschev )  
/// </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>)"; 
// Get the source  
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]; 
} 

 
/// <summary> 
///  To obtain HTML Source of information (Porschev) 
/// </summary> 
/// <param name="url"> Get the address </param> 
/// <returns>HTML The 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 articles: