获取ip

/// <summary>
/// 获取本机的上网IP
/// </summary>
/// <returns></returns>
public static string GetConnectNetAddress()
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址
Uri uri = new Uri(strUrl);
WebRequest webreq = WebRequest.Create(uri);
Stream s = webreq.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站返回的数据 格式:您的IP地址是:[x.x.x.x]
int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "").Replace("<", "");
return ip;
}

posted @ 2015-08-17 14:24  caijinhao  阅读(200)  评论(0编辑  收藏  举报