我如何获得的HttpWebRequest的本地端口号? [英] How do I get the local port number of an HttpWebRequest?

查看:1468
本文介绍了我如何获得的HttpWebRequest的本地端口号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个长期运行的要求与HttpWebRequest的异步。当请求正在运行,我希望能够获得请求的本地端口(例如,一个客户端上,而不是服务器)。我怎么做?

I'm making a long-running request with an HttpWebRequest asynchronously. While the request is running, I'd like to be able to get the local port of the request (ie, the one on the client, not the server). How do I do that?

我看着HttpWebRequest.ServicePoint.BindIPEndPointDelegate,但这似乎只是让调用者指定本地阿迪/端口。理想情况下,我想,让HttpWebRequest的正常挑它的本地端口,然后问它什么选择。

I've looked at HttpWebRequest.ServicePoint.BindIPEndPointDelegate, but that just seems to allow the caller to specify the local addy/port. Ideally, I'd like to allow HttpWebRequest to pick its local port normally and then ask it what it chose.

推荐答案

不幸的是,的HttpWebRequest 类真的不公开,你所要求的。

Unfortunately, the HttpWebRequest class really does not expose what you are asking for.

我不建议我会在下面说,在生产中使用。它的只是编程的乐趣

I don't recommend what I'll say below to be used in production. It's just programmer fun.

的ServicePoint 类有一个私有的 的NetworkStream 成员名为 m_NetworkStream ,到内部曝光属性的NetworkStream

The ServicePoint class has a private NetworkStream member called m_NetworkStream, exposed through internal property NetworkStream.

正如你可能已经知道,的NetworkStream 已经公开了一个公共插槽属性,它为您提供了底层的插座,从您可以访问本地IP端点。

As you may already know, NetworkStream already exposes a public Socket property which gives you the underlying socket, from which you can access the local IP endpoint.

所以,唯一的挑战是获得的PropertyInfo ServicePoint.NetworkStream ,得到它的特定值的HttpWebRequest 实例,并从那里它是一条直线。

So the only challenge is to obtain the PropertyInfo for ServicePoint.NetworkStream, get its value for a specific HttpWebRequest instance and from there on it's a straight line.

注意:对于你可能想看看的 Windows HTTP服务,然后可能创建一个围绕它的托管包装(如果没有的话)。

Note: For a proper solution you may want to take a look at Windows HTTP Services and then perhaps create a managed wrapper around it (if there isn't one already).

享受!

这篇关于我如何获得的HttpWebRequest的本地端口号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆