当前位置:编程学习 > C#/ASP.NET >>

c# 获取IP的问题

我的电脑通过路由器上网,我的局域网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();
        } --------------------编程问答-------------------- 懒,,.百度一下,就有很多例子...

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();
    }

--------------------编程问答-------------------- 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抓取数据 --------------------编程问答-------------------- 因为你没有连网,所以获取的是局域网ip,连网后获取的就是外网的ip了 --------------------编程问答-------------------- 再补充一下,AddressList获取的第一个元素是局域网ip,即AddressList[0],第二个是外网的ip,即AddressList[1],当你没有连网时,AddressList只有一个值,即AddressList[0],局域网ip
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,