![](/img/trans.png)
[英]ASP.net web service and Web Api in a single port
[英]How to find the port number of the caller of a web service in ASP.NET?
我有一个在IIS上运行的Web服务(6或7,无所谓),我想知道调用方从其发送请求/调用的端口。
因此,如果客户端在我的Web服务上进行呼叫,我如何从服务器端找出他们从中进行呼叫的端口号?
那是甚至在最低水平上通过的东西吗? 为了清楚起见,我并不是为了回调目的而寻找端口。 它仅用于记录。
如果服务在WCF上,则:
OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
var endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name]
as RemoteEndpointMessageProperty;
if (endpointProperty != null)
{
string sRemoteAddress = endpointProperty.Address;
int nRemotePort = endpointProperty.Port;
}
TCP套接字确实具有发送方端口号的概念,但是在应用程序级别协议中用处不大。 就是说,考虑到OP的最后一段,我认为您正在寻找某种回调客户的方法。 我之前说过的端口不能用于此目的。 异步请求与在单独线程上运行的同步请求相同,没有什么特别的。
您应该能够在Server对象的服务器变量中将其找到为“ REMOTE_PORT”。
但是,此端口几乎总是应该是随机的,并且仅对于客户端正在创建的“请求/响应”对有效。 不应将其用于异步回调。 甚至您的Web服务在调用其他服务时也会使用随机端口号来发起请求。 通信中唯一的静态端口是TCP连接服务器端的接收端口。
某些网络地址转换类型的设备会向您隐藏实际的“发送”端口号。 然后,您将可以访问无用的IP地址和无用的端口号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.