如何使用 C# 修改请求的 HTTP 标头?

发布于 2024-09-28 16:27:08 字数 303 浏览 0 评论 0原文

我试图使用 C# 修改 HTTP 标头。我尝试操纵页面 preinit 事件上的 Request.Headers 。但是当我尝试对标头设置任何内容时,我收到 PlatformNotSupportedException。由于我们无法将新的 NameValueCollection 设置为 Reqeust.Headers,因此我尝试使用以下代码设置该值:

Request.Headers.Set(HttpRequestHeader.UserAgent.ToString(), "some value");

知道如何实现这一点吗?

I was trying to modify a HTTP Header using C#. I tried to manipulate the Request.Headers on Page preinit event. But when i try to set anything to the Headers, i get PlatformNotSupportedException. Since We can not set a new NameValueCollection to Reqeust.Headers, I tried to set the value using following code:

Request.Headers.Set(HttpRequestHeader.UserAgent.ToString(), "some value");

Any idea how can this be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2024-10-05 16:27:08

试试这个:

HttpContext.Current.Request.Headers["User-Agent"] = "Some Value";

编辑:
这可能是你的原因:
http://bigjimindc.blogspot.com/2007/07/ms -kb928365-aspnet-requestheadersadd.html

其中有一个代码片段,它向 Request.Headers 添加一个新标头。也在 Windows 7 32 位操作系统上进行了验证。

但您可能想将行:替换

HttpApplication objApp = (HttpApplication)r_objSender;

为:

HttpApplication objApp = (HttpApplication)HttpContext.Current.ApplicationInstance;

编辑:
要替换现有标头值,请使用:

t.InvokeMember("BaseSet", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, headers, new object[] { "Host", item });

其中“Host”是标头名称。

Try this:

HttpContext.Current.Request.Headers["User-Agent"] = "Some Value";

EDIT:
This could be your reason:
http://bigjimindc.blogspot.com/2007/07/ms-kb928365-aspnet-requestheadersadd.html

There is a code snippet in that, which adds a new header to the Request.Headers. Verified on Windows 7 32 bit OS too.

But you might want to replace the line:

HttpApplication objApp = (HttpApplication)r_objSender;

with:

HttpApplication objApp = (HttpApplication)HttpContext.Current.ApplicationInstance;

EDIT:
To replace the existing Header value, use:

t.InvokeMember("BaseSet", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, headers, new object[] { "Host", item });

where "Host" is a Header name.

相权↑美人 2024-10-05 16:27:08

从链接的博客添加完整的(工作)代码 - 以防博客消失

HttpApplication objApp = (HttpApplication)HttpContext.Current.ApplicationInstance;
HttpRequest Request = (HttpContext)objApp.Context.Request;

//get a reference
NameValueCollection headers = Request.Headers;

//get a type
Type t = headers.GetType();
System.Collections.ArrayList item = new System.Collections.ArrayList();

t.InvokeMember("MakeReadWrite",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers,null);
t.InvokeMember("InvalidateCachedArrays",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers,null);
item.Add("CUSTOM_HEADER_VALUE");
t.InvokeMember("BaseAdd",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers, new object[]{"CUSTOM_HEADER_NAME",item});
t.InvokeMember("MakeReadOnly",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers,null);

Adding the complete (working) code from the linked blog - incase that blog vanishes

HttpApplication objApp = (HttpApplication)HttpContext.Current.ApplicationInstance;
HttpRequest Request = (HttpContext)objApp.Context.Request;

//get a reference
NameValueCollection headers = Request.Headers;

//get a type
Type t = headers.GetType();
System.Collections.ArrayList item = new System.Collections.ArrayList();

t.InvokeMember("MakeReadWrite",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers,null);
t.InvokeMember("InvalidateCachedArrays",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers,null);
item.Add("CUSTOM_HEADER_VALUE");
t.InvokeMember("BaseAdd",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers, new object[]{"CUSTOM_HEADER_NAME",item});
t.InvokeMember("MakeReadOnly",BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,null,headers,null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文