Implementation Method: capture IP addresses through regular expressions to display website data ....
Friendly reminder: This method is not highly 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.
# 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