.net获取ip地址
/// 獲得客戶端IP
??? ///
??? /// <returns></returns>
??? private string getIp()
??? {
??????? // 穿過(guò)代理服務(wù)器取遠(yuǎn)程用戶真實(shí)IP地址
??????? string Ip = string.Empty;
??????? if (Request.ServerVariables["HTTP_VIA"] != null)
??????? {
??????????? if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
??????????? {
??????????????? if (Request.ServerVariables["HTTP_CLIENT_IP"] != null)
??????????????????? Ip = Request.ServerVariables["HTTP_CLIENT_IP"].ToString();
??????????????? else
??????????????????? if (Request.ServerVariables["REMOTE_ADDR"] != null)
??????????????????????? Ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
??????????????????? else
??????????????????????? Ip = "202.96.134.133";
??????????? }
??????????? else
??????????????? Ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
??????? }
??????? else if (Request.ServerVariables["REMOTE_ADDR"] != null)
??????? {
??????????? Ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
??????? }
??????? else
??????? {
??????????? Ip = "202.96.134.133";
??????? }
??????? return Ip;
??? }
?
?
?
?
?
//控制臺(tái)應(yīng)用程序
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace ConsoleApplication1
{
??? class Program
??? {
??????? static void Main(string[] args)
??????? {
??????????? string hostName = Dns.GetHostName(); //獲取本機(jī)主機(jī)名
??????????? Console.WriteLine("Local hostname: {0}", hostName);
??????????? IPHostEntry myself = Dns.GetHostEntry(hostName);//獲取本機(jī)ip地址信息
??????????? foreach (IPAddress address in myself.AddressList)
??????????? {
??????????????? Console.WriteLine("IP Address: {0}", address.ToString());
??????????? }
??????? }
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/lksa/archive/2010/03/01/1675812.html
總結(jié)
以上是生活随笔為你收集整理的.net获取ip地址的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 大天使何时下架
- 下一篇: Python学习笔记二:布尔表达式