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;
}