c# 获取IP的问题

waslee 2010-12-13 08:16:39
我的电脑通过路由器上网,我的局域网IP为192.168.1.100

我的程序运行如下代码显现的是192.168.1.100 请问如何显示为外网的IP?
//获取IP
private string LocalIP()
{
string localHostName;
IPHostEntry localHostEntry;
IPAddress[] addrList;

int i;
string strTemp = "";

localHostName = Dns.GetHostName();
localHostEntry = Dns.GetHostByName(localHostName);

addrList = localHostEntry.AddressList;
for (i = 0; i < addrList.Length; i++)
{
strTemp = strTemp + addrList[i].ToString() + " ";
}

return strTemp.Trim();
}
...全文
163 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZH620ZH 2010-12-14
  • 打赏
  • 举报
回复
再补充一下,AddressList获取的第一个元素是局域网ip,即AddressList[0],第二个是外网的ip,即AddressList[1],当你没有连网时,AddressList只有一个值,即AddressList[0],局域网ip
ZH620ZH 2010-12-14
  • 打赏
  • 举报
回复
因为你没有连网,所以获取的是局域网ip,连网后获取的就是外网的ip了
wuyq11 2010-12-13
  • 打赏
  • 举报
回复
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();
}
}
// 通过外部网站得到本机的外部IP
static string GetOuterIP()
{
string patt = @"IP: \[(? <IP>[0-9\.]*)\]";
string url = "";
return Regex.Match(GetPage(url), patt).Groups["IP"].Value;
}

static void Main()
{
foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
Console.WriteLine(ip);
}
Console.WriteLine();
Console.WriteLine(GetOuterIP());
}
通过 Htpwebrequest传值到ip138抓取数据
jmh521 2010-12-13
  • 打赏
  • 举报
回复
懒,,.百度一下,就有很多例子...

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Text;

public partial class test003_Default2 : System.Web.UI.Page
{ //外网
public string getIp()
{
string strHostName = Dns.GetHostName();
IPAddress strAddress = Dns.Resolve(strHostName).AddressList[0];
this.Label1.Text = strAddress.ToString();
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
return ip;
}

//本机

public string GetLocalIp()
{
string hostname;
System.Net.IPHostEntry localhost;
System.Net.IPAddress localaddr;

hostname = System.Net.Dns.GetHostName();
localhost = System.Net.Dns.GetHostByName(hostname);
localaddr = localhost.AddressList[0];
return localaddr.ToString();
}

111,097

社区成员

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

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

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