更改HttpWebRequest的一个实例的URI? [英] Change the Uri of a instance of HttpWebRequest?

查看:129
本文介绍了更改HttpWebRequest的一个实例的URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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:

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

  1. 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.

这篇关于更改HttpWebRequest的一个实例的URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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