C#如何通过截取网页获得外网IP

yumanqing 2009-08-03 04:43:24
获得本机的外网IP,代码如下,可是怎么调试都调试不出来,帮忙看看,谢谢了
 private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = this.GetOuterIP();
}
//----
public string GetOuterIP()
{
string patt = @"IP: \[(?<IP>[0-9\.]*)\]";
string url = "http://www.baidu.com/";
string ip = GetPage(url);
textBox3.Text = ip;
return Regex.Match(ip, patt).Groups["IP"].Value;
}
//获取网页的HTML内容
static string GetPage(string url)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
try
{
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
return sr.ReadToEnd();
}
}
}
catch (System.Exception e)
{
return e.Message;
}
finally
{
req.Abort();
}
}

说明:在WINFORM中测试
...全文
372 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yumanqing 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wuyq11 的回复:]
    Uri  uri = new Uri("http://www.163.com");
    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());
    StreamReader rs= new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding(""));
    string s= rs.ReadToEnd();
    rs.Close();
    req.Abort();
    res.Close();
    System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"IP:\[(? <IP>[0-9\.]*)\]");
    if (m.Success) return m.Groups["IP"].Value;

[/Quote]

的方法报错,
System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)(req.GetResponse());
远程服务器返回错误: (405) 不允许的方法。
psho78 2009-08-04
  • 打赏
  • 举报
回复
俩电脑通过路由器上网,外网IP本来就是同一个的呀
yumanqing 2009-08-04
  • 打赏
  • 举报
回复
楼上的方法是可以取出外网IP,可是两台电脑通过一个路由器上网,怎么取出的外网IP是一样的呢?
暈哥 2009-08-04
  • 打赏
  • 举报
回复

//获取外网IP
public string getNetIP()
{
try
{
Uri uri = new Uri("http://www.3322.org/dyndns/getip");//查本机网络IP的网页
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Timeout = 10000;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new CookieContainer();//伪造cookie
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();
Match m = Regex.Match(s, @"[0-9\.]*");
rs.Close();
res.Close();
req.Abort();
if (m.Success)
return m.Value;
}
catch
{
return "获取失败";
}
return "获取失败";
}


http://www.3322.org/dyndns/getip 直接返回你的外网IP
yumanqing 2009-08-04
  • 打赏
  • 举报
回复
在UP一下
wuyq11 2009-08-03
  • 打赏
  • 举报
回复
Uri uri = new Uri("http://www.163.com");
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());
StreamReader rs= new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding(""));
string s= rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"IP:\[(?<IP>[0-9\.]*)\]");
if (m.Success) return m.Groups["IP"].Value;
zhengzeng 2009-08-03
  • 打赏
  • 举报
回复
协议中包含,有相关的方法可以调用的。
nixiang12 2009-08-03
  • 打赏
  • 举报
回复
路过..貌似有点用...
wuyi8808 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yumanqing 的回复:]
还想问下,通过抓取网页获取IP地址,如果网页打不开呢?那不就惨了,有什么好的网页没有,比如百度
[/Quote]

http://www.123cha.com/

++ 您的ip:[xxx.xx.xx.xxx] 来自: xxx xxx xxx ++
yumanqing 2009-08-03
  • 打赏
  • 举报
回复
还想问下,通过抓取网页获取IP地址,如果网页打不开呢?那不就惨了,有什么好的网页没有,比如百度
CN_Azure 2009-08-03
  • 打赏
  • 举报
回复

//获取外网IP
public IPAddress GetMyIP()
{
//下载数据
WebClient client = new WebClient();
byte[] bytRecv = client.DownloadData("http://www.ip138.com/");
string str = System.Text.Encoding.GetEncoding("gb2312").GetString(bytRecv);
//提取信息
string regexStr = @"(?<=您的IP地址是:)[^<]+";
string myip = Regex.Match(str, regexStr).ToString();
return IPAddress.Parse(myip);
}
//获取本地IP
public string[] GetMyHomeIp()
{
string homename = Dns.GetHostName();
IPAddress[] ipa = Dns.GetHostAddresses(homename);
string[] IPaddrs = new string[ipa.Length];
for (int i = 0; i < ipa.Length; i++)
{
IPaddrs[i] = ipa[i].ToString();
}
return IPaddrs;
}
//Get
public void GetIP()
{
string homename = Dns.GetHostName();
IPHostEntry homeIplist = Dns.GetHostByName(homename);
IPAddress [] IpList = homeIplist.AddressList;
for (int i = 0; i < IpList.Length; i++)
{
Debug.WriteLine(IpList[i].ToString());
Debug.WriteLine(IpList[i].Address);
Debug.WriteLine(IpList[i].AddressFamily);
}

}

yumanqing 2009-08-03
  • 打赏
  • 举报
回复
自己UP一下,在线等

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧