WebApi中,怎么获取接口调用者的外网ip地址
答案:2 悬赏:70 手机版
解决时间 2021-01-28 22:27
- 提问者网友:我是女神我骄傲
- 2021-01-28 07:42
WebApi中,怎么获取接口调用者的外网ip地址
最佳答案
- 五星知识达人网友:傲气稳了全场
- 2021-01-28 08:26
It's possible to do that, but not very discoverable - you need to use
the property bag from the incoming request, and the property you need
to access depends on whether you're using the Web API under IIS
(webhosted) or self-hosted. The code below shows how this can be done.
private string GetClientIp(HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty prop;
prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
return prop.Address;
}
return null;
}
the property bag from the incoming request, and the property you need
to access depends on whether you're using the Web API under IIS
(webhosted) or self-hosted. The code below shows how this can be done.
private string GetClientIp(HttpRequestMessage request)
{
if (request.Properties.ContainsKey("MS_HttpContext"))
{
return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
}
if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty prop;
prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
return prop.Address;
}
return null;
}
全部回答
- 1楼网友:思契十里
- 2021-01-28 08:55
不是默认的 wsdl 都会给一个 transport 的地址么,那个 url 中应该有地址,当然可能不是 ip 而是服务器域名,当然也有可能人家没有把 url 直接写在 wsdl 中,但会另外在文档中告诉你 url 地址。你为什么需要知道 ip 地址而不直接用域名?
从分布式的松耦合设计中我们了解到,客户端应该只看 wsdl 再加上额外提供的可选的 url (如果 wsdl 中没有提供这个 transport 配置的话)就应该可以完成与服务器的数据交换,不再需要其它条件。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯