本文主要是介绍C# VB.NET WebApi获取客户端的IP地址,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、需求 : 需要根据IP地址不同返回不同的数据接口。
二、实现:新建一个类模块
Public Module HttpRequestMessageExtensionsPrivate Const HttpContext As String = "MS_HttpContext"Private Const RemoteEndpointMessage As String = "System.ServiceModel.Channels.RemoteEndpointMessageProperty"Private Const OwinContext As String = "MS_OwinContext"<System.Runtime.CompilerServices.Extension>Public Function GetClientIpAddress(ByVal request As HttpRequestMessage) As StringIf request.Properties.ContainsKey(HttpContext) ThenDim ctx As Object = request.Properties(HttpContext)If ctx IsNot Nothing ThenReturn ctx.Request.UserHostAddressEnd IfEnd IfIf request.Properties.ContainsKey(RemoteEndpointMessage) ThenDim remoteEndpoint As Object = request.Properties(RemoteEndpointMessage)If remoteEndpoint IsNot Nothing ThenReturn remoteEndpoint.AddressEnd IfEnd IfIf request.Properties.ContainsKey(OwinContext) ThenDim owinContext_Conflict As Object = request.Properties(OwinContext)If owinContext_Conflict IsNot Nothing ThenReturn owinContext_Conflict.Request.RemoteIpAddressEnd IfEnd IfReturn NothingEnd Function
End Module
三、使用
Debug.Print(Request.GetClientIpAddress())
以上就是WebApi 获取客户端请求的IP过程。
这篇关于C# VB.NET WebApi获取客户端的IP地址的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!