简体   繁体   English

在ASP.Net中将流写入响应

[英]Writing a stream to the response in ASP.Net

Ok just want to clarify something with my solution. 好的,只是想用我的解决方案澄清一下。

I have a requirement to grab a file from a respository somewhere, this repository requires a session token to be passed in the form of a cookie along with the request for the file. 我需要从某个地方的存储库中获取文件,该存储库要求将会话令牌以cookie的形式与文件请求一起传递。

I am authenticating the user against this repository and storing the session token in the users cookie collection for my application when the user first logs onto my application. 我正在根据此存储库对用户进行身份验证,并在用户首次登录我的应用程序时将会话令牌存储在我的应用程序的用户cookie集合中。

Problem is the cookie will not get sent to the repository when a user tried to access a file because the repository is on a different URL and domain. 问题是,由于存储库位于不同的URL和域上,因此用户尝试访问文件时cookie不会发送到存储库。 Therefore I am creating a new http request, appending the cookie and getting the response stream back. 因此,我正在创建一个新的http请求,附加cookie并获取响应流。

I now need to send this response stream back to the user, headers and all (as this response stream will contain the headers for the file the user is trying to access) 我现在需要将此响应流发送回用户,标头和所有消息(因为此响应流将包含用户尝试访问的文件的标头)

Can I use this: 我可以用这个吗:

            string session = cookie.Value;
            StreamReader reader = new StreamReader(Utility.GetLinkStream(url, session));
            Context.Response.ClearHeaders();
            Context.Response.Clear();
            Context.Response.Write(reader.ReadToEnd());

Essentially the call to Utility.GetLinkStream goes off and creates a http request then returns the stream of the response. 本质上,对Utility.GetLinkStream的调用会关闭并创建一个http请求,然后返回响应流。 Will the call to Write write out the whole response headers and all, or is there a better way to acheive this? 写调用会写出整个响应头以及所有响应头,还是有更好的方法来实现?

Response.Write() will only write content, you have to set the headers before calling this. Response.Write()将仅写入内容,您必须在调用此方法之前设置标题。 You could enumerate the headers from the WebResponse and add them to the Response.Headers manually. 您可以枚举WebResponse中的标头,然后将它们手动添加到Response.Headers中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM