怎么样获取本机公网IP

luozhy 2008-12-24 05:05:53
怎么样获取本机公网IP

开发工具vs2005 语言 vc++.net
...全文
625 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mochencui 2008-12-25
  • 打赏
  • 举报
回复
需要找到一个公网的服务地址,向他请求获得返回结果


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{


string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了

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

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

sr.Close();
s.Close();
Console.ReadLine();

}
}
}
luozhy 2008-12-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 oyljerry 的回复:]
需要找到一个公网的服务地址,向他请求获得返回结果
[/Quote]
能不能说详细点啊?
jiang_jiajia10 2008-12-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 oyljerry 的回复:]
需要找到一个公网的服务地址,向他请求获得返回结果
[/Quote]
UP
oyljerry 2008-12-25
  • 打赏
  • 举报
回复
需要找到一个公网的服务地址,向他请求获得返回结果
luozhy 2008-12-25
  • 打赏
  • 举报
回复
还有没有人帮忙解决下 啊?
ijwsoft 2008-12-25
  • 打赏
  • 举报
回复
" 开始 "----" 运行 "-----" cmd "-----" ipconfig ",这么操作也可以获得你的IP地址啊
sunnyboy008 2008-12-24
  • 打赏
  • 举报
回复
帮顶,哈哈
海倍娜楽 2008-12-24
  • 打赏
  • 举报
回复
/// <summary>
/// 获取本地IP
/// </summary>
/// <returns></returns>
public static string GetIP()
{
IPHostEntry tempHost = new IPHostEntry();
tempHost = Dns.GetHostEntry(Dns.GetHostName());
if (tempHost.AddressList.Length == 1)
{
return tempHost.AddressList[0].ToString();
}
else
{
return tempHost.AddressList[1].ToString();
}
}

要导入命名空间:using System.Net;
C++我也玩不转...等待C++高手的出现^_^
SilverNet 2008-12-24
  • 打赏
  • 举报
回复
我不会c++只有C#的,我这个办法比较取巧了,通过IP138这个网站获得的。

static void Main(string[] args)
{
string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了

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

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

sr.Close();
s.Close();
Console.ReadLine();




}

7,540

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 VC.NET
社区管理员
  • VC.NET社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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