C#编程获取IP地址的方法示例
作者:pan_junbiao 时间:2023-06-16 21:18: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);
}
PS:这里再为大家推荐几款IP相关工具供大家参考使用:
IP地址归属地在线查询工具:
http://tools.jb51.net/aideddesign/ipcha
在线IP地址/子网掩码计算与转换工具:
http://tools.jb51.net/aideddesign/ip_net_calc
在线网络计算器|TCP/IP子网掩码计算与换算工具:
http://tools.jb51.net/aideddesign/ipcalc
希望本文所述对大家C#程序设计有所帮助。
标签:C#,IP地址
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java静态泛型使用方法实例解析
2023-03-31 00:41:35
详细解读JAVA多线程实现的三种方式
2022-01-14 04:35:31
C#判断字符串中是否包含指定字符串及contains与indexof方法效率问题
2022-07-06 19:04:51
![](https://img.aspxhome.com/file/2023/6/72466_0s.png)
WPF使用DrawingContext实现二维绘图
2021-06-08 15:04:27
DevExpress GridView自动滚动效果
2022-01-19 09:08:54
![](https://img.aspxhome.com/file/2023/4/102224_0s.gif)
js 交互在Flutter 中使用 webview_flutter
2023-07-20 22:40:14
![](https://img.aspxhome.com/file/2023/5/91785_0s.jpg)
C# 获取程序集版本、文件版本
2022-12-14 13:47:31
Android实现Flip翻转动画效果
2022-05-11 20:08:57
java加密算法分享(rsa解密、对称加密、md5加密)
2021-08-30 16:22:08
javac -encoding 用法详解
2022-06-28 08:58:08
C#使用Aspose.Cells创建和读取Excel文件
2022-11-24 17:47:23
![](https://img.aspxhome.com/file/2023/2/75592_0s.jpg)
spring系列笔记之常用注解
2022-02-21 16:15:04
Java 深拷贝与浅拷贝的分析
2023-07-30 14:13:13
Spring Security配置保姆级教程
2023-11-07 11:46:56
C#多线程处理多个队列数据的方法
2021-12-26 19:31:02
Spring集成Druid连接池及监控配置的全过程
2023-08-25 05:22:20
![](https://img.aspxhome.com/file/2023/8/63448_0s.png)
Android WebView那些坑之上传文件示例
2022-10-29 02:31:15
Java中检查字符串是否以特定字符结尾
2021-08-07 20:04:47
springboot整合腾讯云短信开箱即用的示例代码
2023-04-02 06:06:38
![](https://img.aspxhome.com/file/2023/2/113422_0s.png)
Spring中Bean扫描原理详情
2022-05-26 04:33:14
![](https://img.aspxhome.com/file/2023/8/75488_0s.png)