远程计算机IP地址如何获取,获取远程主机的IP地址

e643ae214b55fab93afd48e0880fab24.png

湖上湖

该解决方案还涵盖了使用Owin自托管的Web API。部分地从这里。您可以在其中创建一个私有方法,ApiController无论您如何托管Web API,该方法都将返回远程IP地址: private const string HttpContext = "MS_HttpContext"; private const string RemoteEndpointMessage =     "System.ServiceModel.Channels.RemoteEndpointMessageProperty"; private const string OwinContext = "MS_OwinContext"; private string GetClientIp(HttpRequestMessage request) {       // Web-hosting       if (request.Properties.ContainsKey(HttpContext ))       {            HttpContextWrapper ctx =                 (HttpContextWrapper)request.Properties[HttpContext];            if (ctx != null)            {                return ctx.Request.UserHostAddress;            }       }       // Self-hosting       if (request.Properties.ContainsKey(RemoteEndpointMessage))       {            RemoteEndpointMessageProperty remoteEndpoint =                (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessage];            if (remoteEndpoint != null)            {                return remoteEndpoint.Address;            }        }       // Self-hosting using Owin       if (request.Properties.ContainsKey(OwinContext))       {           OwinContext owinContext = (OwinContext)request.Properties[OwinContext];           if (owinContext != null)           {               return owinContext.Request.RemoteIpAddress;           }       }        return null; }所需参考资料:HttpContextWrapper -System.Web.dllRemoteEndpointMessageProperty -System.ServiceModel.dllOwinContext -Microsoft.Owin.dll(如果使用Owin包,则已经有了)此解决方案的一个小问题是,当您在运行时实际上只使用其中的一种情况时,必须为所有3种情况加载库。作为建议在这里,这可以通过使用可以克服dynamic变量。您也可以将GetClientIpAddress方法编写为的扩展HttpRequestMethod。using System.Net.Http;public static class HttpRequestMessageExtensions{    private const string HttpContext = "MS_HttpContext";    private const string RemoteEndpointMessage =        "System.ServiceModel.Channels.RemoteEndpointMessageProperty";    private const string OwinContext = "MS_OwinContext";    public static string GetClientIpAddress(this HttpRequestMessage request)    {       // Web-hosting. Needs reference to System.Web.dll       if (request.Properties.ContainsKey(HttpContext))       {           dynamic ctx = request.Properties[HttpContext];           if (ctx != null)           {               return ctx.Request.UserHostAddress;           }       }       // Self-hosting. Needs reference to System.ServiceModel.dll.        if (request.Properties.ContainsKey(RemoteEndpointMessage))       {            dynamic remoteEndpoint = request.Properties[RemoteEndpointMessage];            if (remoteEndpoint != null)            {                return remoteEndpoint.Address;            }        }       // Self-hosting using Owin. Needs reference to Microsoft.Owin.dll.        if (request.Properties.ContainsKey(OwinContext))       {           dynamic owinContext = request.Properties[OwinContext];           if (owinContext != null)           {               return owinContext.Request.RemoteIpAddress;           }       }        return null;    }}现在您可以像这样使用它:public class TestController : ApiController{    [HttpPost]    [ActionName("TestRemoteIp")]    public string TestRemoteIp()    {        return Request.GetClientIpAddress();    }}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值