热衷于在 asp.net 中获取自定义 http-header?
我在一台服务器上有一个 asp.net 应用程序。我在 Page_Load 的服务器端添加了代码:
Response.AddHeader("key", "password-key-from-hotel");
在客户端,我有一个表单:
<form ... action="www.link-to-another-domaint" >
<input type="hidden" id="asd" value="fgh" >
....
</form>
<script type="text/javascript">
document.forms[0].submit();
</script>
然后在另一个域上 - 还有我的其他应用程序 - 我试图通过以下代码获取 hedaer“密钥”:
Request.Headers["key"].ToString();
但是没有这样的标头。有决定吗?我的错误在哪里?
I have an asp.net appliction on the one server. There I've added code on server-side in Page_Load:
Response.AddHeader("key", "password-key-from-hotel");
On the client side I have a form:
<form ... action="www.link-to-another-domaint" >
<input type="hidden" id="asd" value="fgh" >
....
</form>
<script type="text/javascript">
document.forms[0].submit();
</script>
Then on the other domain - there is also my other application - I'm trying to get the hedaer "key" by this code:
Request.Headers["key"].ToString();
But there is no such header. Is there is a desicion? Where is my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我了解,您需要请求字段,而不是标头。尝试:
as i understood you need request field, not header. try:
您从服务器端添加 http 标头,然后尝试从客户端发布表单。
所以,你失去了你的头。
AFAIK,您无法通过表单提交从客户端添加 http 标头(例外可能是 XHR 和其他插件;但看来您的帖子是跨域的,因此 ajax 无法工作)。
我不明白这样做的全部原因,但将自定义标头从一个页面传递到另一个页面的最简单方法是使用 Server.Transfer 方法。
源页面:
目标页面(甚至另一个域):
这应该可行。但是
Server.Transfer
方法有其自己的限制。You're adding http header from the server-side and then trying to post form from the client-side.
So, you lose your header.
AFAIK, you cannot add http header from the client-side with form submit (as an exeption could be XHR and other plugins; but it seems, your post is cross-domain, so ajax won't work).
I don't understand the whole reason of doing it, but the easiest way to pass custom header from one page to another is to use
Server.Transfer
method.Source page:
Destination page (even another domain):
This should work. But
Server.Transfer
method has its own limitations.