アットウィキロゴ

GetGlobalIP

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
 
namespace GlobalIP
{
    class GetGIP
    {
        public GetGIP()
        {
 
        }
 
        /// <summary>
        /// グローバルIPアドレスを取得します。
        /// </summary>
        /// <returns></returns>
        static public IPAddress Get()
        {
            //string gip = "127.0.0.1";
            IPAddress gIP = IPAddress.Parse("127.0.0.1");
            WebClient wc = new WebClient();
 
            Stream st = wc.OpenRead("http://checkip.dyndns.org/");
 
            Encoding enc = Encoding.GetEncoding("Shift_JIS");
            StreamReader sr = new StreamReader(st, enc);
            string html = sr.ReadToEnd();
            sr.Close();
 
            st.Close();
 
            StringReader rs = new StringReader(html);
            Regex regex = new Regex("[0-9]{1,3}([.][0-9]{1,3}){3}");
            string gip = "127.0.0.1";
 
            while (rs.Peek() > -1)
            {
                string line = rs.ReadLine();
                if (regex.IsMatch(line))
                {
                    gip = regex.Match(line).ToString();
                }
 
            }
 
            gIP = IPAddress.Parse(gip);
 
            return (gIP);
        }
    }
}
 

タグ:

+ タグ編集
  • タグ:
最終更新:2010年12月08日 16:09
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。