日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

获取远程计算机动态ip,c# - 获取远程主机的IP地址

發(fā)布時間:2024/1/23 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 获取远程计算机动态ip,c# - 获取远程主机的IP地址 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

此解決方案還涵蓋使用Owin自托管的Web API。 部分來自這里。

您可以在dynamic中創(chuàng)建一個私有方法,無論您如何托管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;

}

所需參考:

dynamic - System.Web.dll

dynamic - System.ServiceModel.dll

dynamic - 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;

}

}

現(xiàn)在您可以像這樣使用它:

public class TestController : ApiController

{

[HttpPost]

[ActionName("TestRemoteIp")]

public string TestRemoteIp()

{

return Request.GetClientIpAddress();

}

}

總結(jié)

以上是生活随笔為你收集整理的获取远程计算机动态ip,c# - 获取远程主机的IP地址的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。