大家都搜到的类:
Code
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
///getIp 的摘要说明
/// </summary>
public class getIp
{
public getIp()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static string netIP()
{
try
{
Uri uri = new Uri("http://www.ikaka.com/ip/index.asp");//查本机网络IP的网页
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new CookieContainer();
req.GetRequestStream().Write(new byte [0], 0, 0);
HttpWebResponse res = (HttpWebResponse)(req.GetResponse());
StreamReader rs = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("GB18030"));
string s = rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
//int a=s.IndexOf("<div class=\"tis_bg_b\"");
//s = s.Substring(a, 100);
//return s;
Match m = Regex.Match(s, @"IP:[(?<IP>[0-9.]*)]");
if (m.Success) return m.Groups["IP"].Value;
string strnetIP = string.Empty;
return strnetIP;
}
catch (Exception e1)
{
return e1.Message;
}
}
}
其中注释的是我自己写的
我改写后的 只是一点很小的变化,也许有人看了会觉得蠢 但是我感觉常高兴,原来也遇到过 搜到代码后测试不成功救泄气了,最后还是用的本地ip
Code
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
///getIp 的摘要说明
/// </summary>
public class getIp
{
public getIp()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static string netIP()
{
try
{
Uri uri = new Uri("http://www.ikaka.com/ip/index.asp");//查本机网络IP的网页
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new CookieContainer();
req.GetRequestStream().Write(new byte [0], 0, 0);
HttpWebResponse res = (HttpWebResponse)(req.GetResponse());
StreamReader rs = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("GB18030"));
string s = rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
int a=s.IndexOf("<div class=\"tis_bg_b\"");
s = s.Substring(a, 100);
return s;
//Match m = Regex.Match(s, @"IP:[(?<IP>[0-9.]*)]");
//if (m.Success) return m.Groups["IP"].Value;
//string strnetIP = string.Empty;
//return strnetIP;
}
catch (Exception e1)
{
return e1.Message;
}
}
}
最后返回的界面:
查看源文件是界面如下:
就是label1中的标签中显示的字符串是:就是注释中的s=<div class="tis_bg_b">++ 你的IP: [<span class="b"> 60.174.143.68</span>,来自:<span class="b">安徽省马鞍山市电信<
注意大家:有于时间原因没有将字符串s中的ip提出,相信大家很好做的,
有什么好的方法?
请评论!!!!!!留言
谢谢!