c# - 在 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);
}
}
..但我找不到任何东西来获得港口。
编辑。感谢蒂姆的精彩评论:
这是完整的工作流程:
- 客户端启动,打开一个套接字
- 嘿webservice 我的IP 和端口是什么?
- 这是 1.2.3.4:1234
- 嘿其他人,我会在 1.2.3.4:1234 上收听(尽管对我来说它看起来像 9.9.9.9:9999
- 好的,我将连接到 1.2.3.4:1234
- 大家开心,喝啤酒
解决方案
我得到了 context.Request.ServerVariables["REMOTE_PORT"]。
没有什么,只是猜测和到处寻找:)
推荐阅读
- python - 为 Pandas Dataframe 中的重复集创建规则
- shibboleth - Shibboleth SP 如何在 Shibboleth 服务不停机的情况下配置 IdP 元数据文件?
- javascript - 如何在 vue js 中更新本地存储中的对象?
- android - 如何在 Kotlin 中使用 MPAndroidChart 为条形图添加带有一些详细信息的标记?
- android - android-19 的 Qt 构建
- javascript - SyntaxError: 缺少 catch 或 finally 尝试后
- java - 如何复制apache poi中的列?
- linux - 在虚拟机之间共享 linux 日志文件
- ios - 在 MacOS 上公开 safaridriver 以用作 Window 机器的 RemoteWebDriver
- mysql - Heroku 日志说 ReferenceError: PORT is not defined