static string GetPublicIpAdress()
{
string uri ="http://city.ip138.com/city0.asp";
WebRequest req = WebRequest.Create(uri);
WebResponse res = req.GetResponse();
Stream sm = res.GetResponseStream();
StreamReader reader = new StreamReader(sm,Encoding.GetEncoding("GBK"));
string ip= reader.ReadToEnd();
reader.Close();
StringBuilder txt = new StringBuilder();
bool state=false;
foreach (char c in ip)
{
if (c == ']') return txt.ToString();
if (state) txt.Append(c);
if (c == '[') state = true;
}
return txt.ToString();
}
C#之获取公网IP
最新推荐文章于 2022-05-05 15:15:47 发布