C#编程获取IP地址或网站域名归属地
1、打开VS2010,设计软件界面
2、 /// <summary> /// 获取指定网页源码 /// </summary> /// <param name="url">指定的网页</param> /// <returns>网页源码</returns> private string getHtml(string url) { string strHTML = ""; WebClient myWebClient = new WebClient(); Stream myStream = myWebClient.OpenRead(url); StreamReader sr = new StreamReader(myStream, Encoding.GetEncoding("gb2312")); strHTML = sr.ReadToEnd(); myStream.Close(); return strHTML; }
3、 /// <summary> /// 获取诔罨租磊IP归属地 /// </summary> /// <param name="strIP">IP地址</param> /// <returns>IP归属地</returns> private string GetIPAdress(string strIP) { string s = getHtml("http://www.ip138.com/ips1388.asp?ip=" + strIP + "&action=2"); int a = s.IndexOf("本站主数据:") + "本站主数据:".Length; int b = s.IndexOf("参考数据一"); return s.Substring(a, b - a).Replace("</li><li>", ""); }
4、 /// <summary> /// 获取噩荜务圃网站域名归属地 /// </summary> /// <param name="strWWW">网站域名</param> /// <returns>网站域名归属地</returns> private string GetWWWIP(string strWWW) { if (string.IsNullOrEmpty(strWWW)) return null; string s = getHtml("http://www.ip138.com/ips1388.asp?ip=" + strWWW + "&action=2"); int a = s.IndexOf("<h1><font color=\"blue\">") + "<h1><font color=\"blue\">".Length; int b = s.IndexOf("</font></h1></td>"); return s.Substring(a, b - a).Replace(tbWWW.Text, "").Replace(" >> ", "").Replace(" ", ""); }
5、调试运行,修改错误