更改HttpWebRequest的一个实例的URI?实例、HttpWebRequest、URI

2023-09-05 01:51:43 作者:想太多,对心脏不好

我有一个HttpWebRequest的实例是我截取的一个事件。 我想编辑URL的请求被发送之前,但我找不到这样做的方式。 该物业RequestUri是只读的。

I have an instance of an HttpWebRequest that I'm intercepting in an event. I would like to edit the url before the request is sent but I can't find a way of doing this. The property RequestUri is read only.

我已经想到了一些方法,但似乎无法找到一个有效的解决方案: - 使用反射来设置的值? - 创建一个新的请求,然后克隆的所有属性。不知道该怎么做。

I've thought of a few ways but can't seem to find a working solution: -Using reflection to set the value ? -Creating a new request and then cloning all properties. not sure how to do that.

推荐答案

如果你想在HTTP协议中的条款,每个请求是无状态的/独一无二的。以一个请求链接到另一个的唯一方法是通过编程像饼干,但对HTTP协议本身的要求是独一无二的。

If you think in terms of the HTTP protocol, each request is stateless / unique. The only way to link one request to another is programmatically through something like Cookies, but to the HTTP protocol itself the request is unique.

我觉得HttpWebRequest对象在设计时考虑到这一点。每个HttpWebRequest的重presents到一个URL一个唯一的电话,你建立的参数,该呼叫。如果你想要做的另一个请求到一个不同的URL,你将创建一个新的HttpWebRequest,并通过它,你正在使用的状态信息,如:饼干容器,标题信息等

I think the HttpWebRequest object was designed with this in mind. Each HttpWebRequest represents one unique call to a URL and you build up the parameters for that call. If you want to do another request to a different URL you would create a new HttpWebRequest and pass it the state information you are using, ie: Cookie container, header information etc.

的很长的解释,以这样的对象被设计成具有一个只读网址,来处理它是唯一的方法:

The long winded answer to this is the object is designed to have a read only url and the only way to handle it is to:

使用用心体会黑客,如你已经做了,如果你确实需要使用给定的HttpWebRequest对象你。

Use a little reflection hack, such as you have already done, if you absolutely need to use the given HttpWebRequest object you have.

创建一个新的HttpWebRequest(WebRequest.Create()),并复制你的状态信息到新的要求。

Create a new HttpWebRequest (WebRequest.Create()) and copy your state information over to the new request.