mvc中的反向代理
我必须在 mvc 中实现类似代理的东西来发送另一台服务器上的用户文件。我发现这个类:
public class ProxyHandler : IHttpHandler, IRouteHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
string str = "http://download.thinkbroadband.com/100MB.zip";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(str);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
HttpResponse res = context.Response;
res.Write(reader.ReadToEnd());
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return this;
}
}
问题是在这个解决方案中,我首先下载文件,然后将下载的文件发送给用户,这不是我想要的。我想在开始下载文件后立即将文件发送给用户,例如在此在线匿名器中 http://bind2.com/
有什么建议如何实现这一点吗?
I have to implement something like proxy in mvc to send user file that is on another server . I found this class :
public class ProxyHandler : IHttpHandler, IRouteHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
string str = "http://download.thinkbroadband.com/100MB.zip";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(str);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
HttpResponse res = context.Response;
res.Write(reader.ReadToEnd());
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return this;
}
}
The problem is in this solution i first download file and then send downloded file to user with is not what i want. I want to send file to user as soon as I start to download it like in this Online Anonymizer for example
http://bind2.com/
Any suggestions how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
上面示例中的以下行:
相当于:
即,在将整个 Web 响应传递到客户端之前,将下载整个 Web 响应并将其存储在字符串中。
相反,您应该使用类似以下内容的内容:(
流复制代码取自 在两个 Stream 实例之间复制的最佳方法 - C#)
这会以 32 KB 块的形式复制流 - 您可能希望减小该块的大小。
The following line in the above sample:
Is equivalent to:
I.e. the entire web response is being downloaded and stored in a string before it is being passed to the client.
Instead you should use something like the following:
(Stream copying code taken from Best way to copy between two Stream instances - C#)
This copies the stream in 32 KB chunks - you may want to make this chunk size smaller.
您可以通过将文件直接流式传输到用户的响应流来实现此目的。这样,您就不必等待整个文件下载到服务器上才开始将其发送给用户。
这是代码片段:
让我知道它是否有效。
You can achieve this by streaming the file directly to the user’s response stream. This way, you don’t have to wait for the entire file to be downloaded on your server before starting to send it to the user.
Here's the code snipped:
Let me know if it works.