首页 > 解决方案 > 在 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. 大家开心,喝啤酒

标签: c#endpointihttphandler

解决方案


我得到了 context.Request.ServerVariables["REMOTE_PORT"]。

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


推荐阅读