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