在 IHttpHandler 中检测远程端口

问题描述

嘿,您如何在 Web 服务器上获取请求的远程端口?

所以我的代码应该是这样的:

public class EndpointReflectionHandler : IHttpHandler
{
    public new void ProcessRequest(HttpContext context)
    {
        string address = context.Request.UserHostAddress;
        int port = context.Request.UserHostPort;            //it's not there

        context.Response.ContentType = "text/plain";
        context.Response.Write(address + ":" + port);
    }
}

...但我找不到任何东西来获取端口。

编辑。感谢蒂姆的精彩评论

完整的工作流程如下:

  1. 客户端启动,打开一个套接
  2. 嘿 webservice,我的 IP 和端口是什么?
  3. 现在是 1.2.3.4:1234
  4. 嘿其他人,我会听 1.2.3.4:1234(尽管在我看来它看起来像 9.9.9.9:9999
  5. 好的,我会连接到 1.2.3.4:1234
  6. 大家开心,来杯啤酒

解决方法

我知道了 context.Request.ServerVariables["REMOTE_PORT"]。

没什么,只是猜测和到处寻找:)