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)
{
StreamReader sr = null;
Stream s = null;
string strUrl = "http://iframe.ip138.com/ic.asp"; //获得IP的网址了
try
{
Uri uri = new Uri(strUrl);
WebRequest wr = WebRequest.Create(uri);
s = wr.GetResponse().GetResponseStream();
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(" ", "")+"\r\n";//找出ip
Console.WriteLine(ip);
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
finally
{
if(sr!=null)
sr.Close();
if (s != null)
s.Close();
}
}
}
}
C#获得本机的公网IP地址
最新推荐文章于 2023-12-20 21:20:48 发布