using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] IpAndAddress=GetIPAndAddress();
Console.WriteLine(
"您的电脑外网\n"+
"IP = {0}\nAddress = {1}", IpAndAddress[0], IpAndAddress[1]
);
while (true) ;
}
// C# IP地址转长整数
// code from : http://space.itpub.net/12639172/viewspace-672871
// 有改动
static string[] GetIPAndAddress()
{
try
{
string strUrl = "http://www.ip138.com/ips138.asp";//这个可以获得调试争取的结果。
// 下面两个网址貌似不是很正确的。
//"http://iframe.ip138.com/ic.asp";
//"http://www.ip138.com/ip2city.asp";
Uri uri = new Uri(strUrl);
WebRequest wr = WebRequest.Create(uri);
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据ITPUB个人空间8k5_8Qj%v.k
int i = all.IndexOf("IP地址是:") + 7;
string tempip = all.Substring(i, 15);
string ip = tempip.Split(new char[] { ']' })[0];
i += ip.Length + 5;
string temAddress = all.Substring(i, 100);
string address = temAddress.Split(new char[] { '<' })[0];
return new string[] { ip, address };
}
catch
{
return new string[]
{
"Error,Cannot connect outside",
"Error,You must online,First"
};
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] IpAndAddress=GetIPAndAddress();
Console.WriteLine(
"您的电脑外网\n"+
"IP = {0}\nAddress = {1}", IpAndAddress[0], IpAndAddress[1]
);
while (true) ;
}
// C# IP地址转长整数
// code from : http://space.itpub.net/12639172/viewspace-672871
// 有改动
static string[] GetIPAndAddress()
{
try
{
string strUrl = "http://www.ip138.com/ips138.asp";//这个可以获得调试争取的结果。
// 下面两个网址貌似不是很正确的。
//"http://iframe.ip138.com/ic.asp";
//"http://www.ip138.com/ip2city.asp";
Uri uri = new Uri(strUrl);
WebRequest wr = WebRequest.Create(uri);
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据ITPUB个人空间8k5_8Qj%v.k
int i = all.IndexOf("IP地址是:") + 7;
string tempip = all.Substring(i, 15);
string ip = tempip.Split(new char[] { ']' })[0];
i += ip.Length + 5;
string temAddress = all.Substring(i, 100);
string address = temAddress.Split(new char[] { '<' })[0];
return new string[] { ip, address };
}
catch
{
return new string[]
{
"Error,Cannot connect outside",
"Error,You must online,First"
};
}
}
}
}