国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#編程獲取IP地址的方法示例

2019-10-29 21:15:20
字體:
來源:轉載
供稿:網友

本文實例講述了C#編程獲取IP地址的方法。分享給大家供大家參考,具體如下:

1、獲取客戶端IP

/// <summary>/// 獲取客戶端Ip/// </summary>/// <returns></returns>public String GetClientIp(){  String clientIP = "";  if (System.Web.HttpContext.Current != null)  {    clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];    if (string.IsNullOrEmpty(clientIP) || (clientIP.ToLower() == "unknown"))    {      clientIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];      if (string.IsNullOrEmpty(clientIP))      {        clientIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];      }    }    else    {      clientIP = clientIP.Split(',')[0];    }  }  return clientIP;}

2、服務器端獲取客戶端請求IP和客戶端機器名稱

/// <summary>/// 服務器端獲取客戶端請求IP和客戶端機器名稱/// </summary>public static void GetClientInfo(){  OperationContext context = OperationContext.Current;  MessageProperties messageProperties = context.IncomingMessageProperties;  RemoteEndpointMessageProperty endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;  HttpRequestMessageProperty requestProperty = messageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;  string clientIp = !string.IsNullOrEmpty(requestProperty.Headers["X-Real-IP"]) ? requestProperty.Headers["X-Real-IP"] : endpointProperty.Address;  string clientName = Environment.MachineName;  Console.WriteLine("ClientIp: " + clientIp + "clientName:" + clientName);}

希望本文所述對大家C#程序設計有所幫助。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麻阳| 上饶县| 萨迦县| 台山市| 芒康县| 凤台县| 日照市| 盘山县| 平昌县| 麦盖提县| 牡丹江市| 鹰潭市| 万年县| 如皋市| 涞水县| 米林县| 益阳市| 察雅县| 凌海市| 定边县| 屏山县| 宝坻区| 错那县| 固原市| 宜兴市| 天门市| 大埔县| 乐安县| 罗山县| 天台县| 龙游县| 霍林郭勒市| 收藏| 都匀市| 大方县| 桐乡市| 理塘县| 皮山县| 镇沅| 丰台区| 浪卡子县|