重定向后保留 HTTP 标头
我正在尝试通过我的ASP.NET应用从Google文档下载文件。
由于我使用的是Oauth,因此我生成了一个签名并进入授权字符串。
从Google Docs文档,要访问其资源,我需要添加:
Authorization: <authorization string>
在请求标题中。
我写了一个片段来进行重定向:
context.Response.AddHeader("GData-Version", "3.0");
context.Response.AddHeader("Host", downloadLinkUrl.Host);
context.Response.AddHeader("Authorization", authorizaString);
context.Response.RedirectLocation = downloadLink;
context.Response.Redirect(downloadLink);
它重定向到下载链接,但是从Firebug可以看出,缺少标题信息。因此,我得到401未获得授权。
一些读取说这是不可能的。周围有黑客吗?
谢谢。
I am trying to download a file from Google Docs, via my ASP.Net app.
Since I am using OAuth, I generated a signature and into authorization string.
From Google Docs documentation, to access it's resource, I will need to add:
Authorization: <authorization string>
in the request header.
I wrote a snippet to do the redirection:
context.Response.AddHeader("GData-Version", "3.0");
context.Response.AddHeader("Host", downloadLinkUrl.Host);
context.Response.AddHeader("Authorization", authorizaString);
context.Response.RedirectLocation = downloadLink;
context.Response.Redirect(downloadLink);
It redirected to the downloadLink, but Header information is missing, as seen from Firebug. Thus I get 401 Not Authorized.
Some read up said that it's not possible. Is there any hack around?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法转发标头。您唯一的选择是尝试在重定向页面上使用 jQuery/javascript 添加标头,然后发出 GET 请求。我什至不确定是否可以,因为 AJAX 请求不能用于文件下载。
事实上..我认为这也行不通。所以 - 我不得不说不,你不能为文件下载执行此操作。但是,您可以从服务器发出请求,然后将其流式传输到客户端吗?
编辑
我过去回答过这个问题:
使用 AJAX 调用的 jQuery 传递文件
因为 jQuery get 是.ajax 的简写形式 - 这可以通过执行指定“get”的 jQuery ajax 调用来实现。尝试一下,让我们知道结果。
http://api.jquery.com/jQuery.ajax#options
对于一些代码看:
在 jQuery AJAX GET 调用中传递请求标头
You cannot forward a header. Your only bet is to try to use jQuery/javascript on a redirect page to add the headers and then make a GET request. I'm not even sure if you can since AJAX requests cant be used for file download.
Actually.. I don't think that will work either. So - I have to say no, you can't do this for a file download. However can you make the request from the server and then stream it to the client?
EDIT
I answered this in the past:
jQuery delivery of a file with AJAX call
Since jQuery get is a shorthand form of .ajax - this MAY work by doing a jQuery ajax call specifying 'get'. Give it a try and let us know the results.
http://api.jquery.com/jQuery.ajax#options
For some code see:
Pass request headers in a jQuery AJAX GET call
在express.js 中,可以使用标头和cookie 来重定向响应:
我相信.NET 中也有类似的东西。
In express.js there is possible to redirect response with both headers and cookies:
I believe in .NET there is something similar.