c# winform 如何获取公网ip,可能是自己拨号,可能是用路由器上网

cannycsy 2009-01-09 01:20:55
c# winform 如何获取公网ip,可能是自己拨号,可能是用路由器上网

取得上网IP有什么办法呢?
...全文
1156 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lmhlove009 2010-04-12
  • 打赏
  • 举报
回复
看看怎么解决
chinafeng2008 2009-09-16
  • 打赏
  • 举报
回复
try
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了

Uri uri = new Uri(strUrl);
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, System.Text.Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据

int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "");//找出i


}
catch (Exception ex)
{

}
这个就正确了。
qq223204228 2009-01-18
  • 打赏
  • 举报
回复
没办法,只有写个服务器端来返回.
优途科技 2009-01-10
  • 打赏
  • 举报
回复
up
再见品月 2009-01-09
  • 打赏
  • 举报
回复
只是想知道。。。访问ip138.com。
在程序中使用的话,就要有LS的方法了。。
解析返回的html。
homejiji 2009-01-09
  • 打赏
  • 举报
回复
关注更好的方法中。。
特别 2009-01-09
  • 打赏
  • 举报
回复

System.Net.WebClient client = new System.Net.WebClient();
client.Encoding =System.Text.Encoding.Default;
string reply = client.DownloadString( "http://www.ip138.com/ip2city.asp" );
MessageBox.Show( reply );

然后截取一下[]之间的内容
yhy0611 2009-01-09
  • 打赏
  • 举报
回复
学习了
Fibona 2009-01-09
  • 打赏
  • 举报
回复
try
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了

Uri uri = new Uri(strUrl);
System.Net.WebRequest wr = System.Net.WebRequest.Create(uri);
System.IO.Stream s = wr.GetResponse().GetResponseStream();
System.IO.StreamReader sr = new System.IO.StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据

int i = all.IndexOf("[") + 1;
string tempip = all.Substring(i, 15);
string ip = tempip.Replace("]", "").Replace(" ", "");//找出i


}
catch (Exception ex)
{

}



还有一种就是通过
HttpWebRequest访问路由器的管理页面,里面有公网IP
冷月孤峰 2009-01-09
  • 打赏
  • 举报
回复
www.ip138.com
suners 2009-01-09
  • 打赏
  • 举报
回复
顶一下
EveryCase 2009-01-09
  • 打赏
  • 举报
回复
ding
zoOoz 2009-01-09
  • 打赏
  • 举报
回复
访问一下www.ip138.com就知道了,如果要通过哦程序读取,用webclient读取www.ip138.com页,去掉其他不必要的信息就可以了
yqlx521 2009-01-09
  • 打赏
  • 举报
回复
给你个网址
还不错http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/200853/112463.html
mafgm 2009-01-09
  • 打赏
  • 举报
回复
System.Net.WebClient client = new System.Net.WebClient();
client.Encoding =System.Text.Encoding.Default;
string reply = client.DownloadString( "http://www.ip138.com/ip2city.asp" );
MessageBox.Show( reply );
------------------------------------------------
正解
CruelYoung123 2009-01-09
  • 打赏
  • 举报
回复
搞个服务器端...去ip138.com提取?

110,533

社区成员

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

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

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