public class getIP
{
/// <summary>
/// 获得IP
/// </summary>
/// <returns></returns>
public static string GetIp()
{
string tempip = "";
try
{
//string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了
string strUrl = "http://iframe.ip138.com/ic.asp"; //获得IP的网址了
Uri uri = new Uri(strUrl);
WebRequest wr = WebRequest.Create(uri);
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据
int start = all.IndexOf("[") + 1;
int end = all.IndexOf("]", start);
tempip = all.Substring(start, end - start);
}
catch (Exception ex)
{
return "";
}
return tempip;
}
/// <summary>
/// 根据ip获取地区信息
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static string GetIpAddress(string ip)
{
string url = "http://www.ip138.com/ips138.asp?ip=" + ip + "&action=2";
string pageAll = Html.GetUrlSource(url, Encoding.Default);
string address = string.Empty;
System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex("<li>本站主数据:(.*?)</li>", System.Text.RegularExpressions.RegexOptions.None);
System.Text.RegularExpressions.MatchCollection mc = re.Matches(pageAll);
string link = string.Empty;
foreach (System.Text.RegularExpressions.Match ma in mc)
{
address = ma.Groups[1].Value;
}
return address;
}
/// <summary>
/// 根据ip获取地区
/// </summary>
/// <returns></returns>
public static string GetIpArea()
{
string Area = "";
try
{
string Ip = getIP.GetIp();
string AreaInfo = getIP.GetIpAddress(Ip);
int start = AreaInfo.IndexOf("省") + 1;
int send = AreaInfo.IndexOf("市");
int end = AreaInfo.IndexOf("市", start);
if (send < 0)
{
send = AreaInfo.IndexOf("州");
end = AreaInfo.IndexOf("州", start);
if (send < 0)
{
Area = AreaInfo.Substring(0, start);
}
else
{
Area = AreaInfo.Substring(start, end - start);
}
}
else
{
Area = AreaInfo.Substring(start, end - start);
}
}
catch
{
Area = "";
}
return Area;
}
}
评论列表: