復(fù)制代碼 代碼如下:
/// summary>
/// 得到真實IP以及所在地詳細信息(Porschev)
/// /summary>
/// returns>/returns>
public string GetIpDetails()
{
//設(shè)置獲取IP地址和國家源碼的網(wǎng)址
string url = "http://www.ip138.com/ips8.asp";
string regStr = "(?=td\\s*align=\\\"center\\\">)[^]*?(?=br/>br/>/td>)";
//IP正則
string ipRegStr = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
//IP地址
string ip = string.Empty;
//國家
string country = string.Empty;
//省市
string adr = string.Empty;
//得到網(wǎng)頁源碼
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);
//得到IP
ip = ma.Value;
int index = html.LastIndexOf(":") + 1;
//得到國家
country = html.Substring(index);
adr = GetAdrByIp(ip);
return "IP:" + ip + " 國家:" + country + " 省市:" + adr;
}
/// summary>
/// 通過IP得到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>)";
//得到網(wǎng)頁源碼
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];
}
復(fù)制代碼 代碼如下:
/// summary>
/// 獲取HTML源碼信息(Porschev)
/// /summary>
/// param name="url">獲取地址/param>
/// returns>HTML源碼/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;
}
您可能感興趣的文章:- C#獲取本機IP地址和Mac地址的方法
- c#獲取本機在局域網(wǎng)ip地址的二種方法
- c#獲取本機的IP地址的代碼
- C#獲取真實IP地址實現(xiàn)方法
- C#獲取計算機名,IP,MAC信息實現(xiàn)代碼
- 用C#編寫獲取遠程IP,MAC的方法
- C#獲取本機IP地址(ipv4)