我的电脑通过路由器上网,我的局域网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();
}
//获取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();
}
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();
}
{
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抓取数据