Preserve HTTP header after redirection
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.
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
In express.js there is possible to redirect response with both headers and cookies:
res.set('mykey', 'myvalue');
res.set('Access-Control-Expose-Headers', 'Set-Cookie, mykey');
res.cookie('mykey', 'myvalue', { maxAge: 900000, httpOnly: true });
res.redirect(302, url);
I believe in .NET there is something similar.
精彩评论