This approach is not very efficient, if used in the web, if used less can be used in this way, if used, or recommend the use of IP library
Copy Code code as follows:
#region # #得到真实IP以及所在地详细信息
<summary>
Get real IP and location details (Porschev)
</summary>
<returns></returns>
public string Getipdetails ()
{
String url = "Http://www.ip138.com/ips8.asp"; Set up a URL to get the IP address and country source
String regstr = "(? <=<td\\s*align=\\\" center\\\ ">) [^<]*?] (?=<br/><br/></td>) ";
String ipregstr = "((2[0-4]\\d|25[0-5]|[ 01]?\\d\\d?) \\.) {3} (2[0-4]\\d|25[0-5]| [01]?\\d\\d?] "; IP Regular
String IP = string. Empty; IP Address
String country = string. Empty; Countries
String ADR = string. Empty; Provinces
String html = gethtml (URL); Get the source page
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; Get IP
int index = HTML. LastIndexOf (":") + 1;
Country = HTML. Substring (index); Get the country
ADR = GETADRBYIP (IP);
Return "IP:" + IP + "Country:" + Country + "Provinces and Cities:" + ADR;
}
#endregion
#region # #通过IP得到IP所在地省市
<summary>
IP location via IP (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>) ";
String html = gethtml (URL); Get the source page
Regex reg = new Regex (REGSTR, regexoptions.none);
Match ma = Reg. Match (HTML);
html = ma. Value;
string[] arr = html. Split (")";
return arr[0];
}
#endregion
#region # #获取HTML源码信息
<summary>
Get HTML source information (Porschev)
</summary>
<param name= "url" > Get address </param>
<returns>html Source </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