调用GetIP getip = Get();
Label1.Text = getip.Ip;
Label2.Text = getip.Country;
public class GetIP
{
private string _ip;//IP
public string Ip
{
get { return _ip; }
set { _ip = value; }
}
private string country;//归属地
public string Country
{
get { return country; }
set { country = value; }
}
}
public static GetIP Get()
{
Uri uri = new Uri(" http://www.ip.cn/getip.php?action=getip&ip_url=");
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new System.Net.CookieContainer();
req.GetRequestStream().Write(new byte[0], 0, 0);
System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)(req.GetResponse());
System.IO.StreamReader rs = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB18030"));
string s = rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
GetIP getip = new GetIP();
int start = s.IndexOf(">");
int end = s.IndexOf("</");
getip.Ip = s.Substring(start + 1, end - start - 1);
getip.Country = s.Substring(s.IndexOf(":") + 1, s.Length - s.IndexOf(":") - 1);
return getip;
}
- 本文标题: 如何获得外网IP地址 ASP.NET
- 文章分类:【.NET/Web】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.