獲取伺服器電腦名:Page.Server.ManchineName
獲取使用者資訊:Page.User
獲取客戶端電腦名:Page.Request.UserHostName
獲取客戶端電腦IP:Page.Request.UserHostAddress
2. 在網路程式設計中的通用方法:
獲取當前電腦名:static System.Net.Dns.GetHostName()
根據電腦名取出全部IP地址:static System.Net.Dns.Resolve(電腦名).AddressList
也可根據IP地址取出電腦名:static System.Net.Dns.Resolve(IP地址).HostName
3. 系統環境類的通用屬性:
當前電腦名:static System.Environment.MachineName
當前電腦所屬網域:static System.Environment.UserDomainName
當前電腦使用者:static System.Environment.UserName
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace ipip
{
/// <summary>
/// Class1 的摘要說明。
/// 獲取本機上網IP和內網IP
/// </summary>
public class Class1
{
private string strgetIP;
public Class1()
{
//
netIP();
getIP();
}
public string renetIP()
{return netIP();}//返回網路IP
public string regetIP()
{return strgetIP;}//返回內網IP
static string netIP()
{
Uri uri = new Uri("http://www.ikaka.com/ip/index.asp");//查本機網路IP的網頁
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new CookieContainer();
req.GetRequestStream().Write(new byte [0], 0, 0);
HttpWebResponse res = (HttpWebResponse)(req.GetResponse());
StreamReader rs = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("GB18030"));
string s = rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
Match m = Regex.Match(s, @"IP:\[(?<IP>[0-9\.]*)\]");
if (m.Success) return m.Groups["IP"].Value;
string strnetIP= string.Empty;
return strnetIP;
}
public string getIP()//注意與static 的區別
{
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;//獲取本機內網IP
strgetIP = addressList[0].ToString();
return strgetIP;
}
}
}