mvc中的反向代理

发布于 2024-10-21 21:19:49 字数 961 浏览 1 评论 0原文

我必须在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒲公英的约定 2024-10-28 21:19:50

上面示例中的以下行:

res.Write(reader.ReadToEnd());

相当于:

string responseString = reader.ReadToEnd();
res.Write(responseString);

即,在将整个 Web 响应传递到客户端之前,将下载整个 Web 响应并将其存储在字符串中。

相反,您应该使用类似以下内容的内容:(

Stream responseStream = response.GetResponseStream();

byte[] buffer = new byte[32768];
while (true)
{
    int read = responseStream.Read(buffer, 0, buffer.Length);
    if (read <= 0)
        return;
    context.Response.OutputStream.Write(buffer, 0, read);
}

流复制代码取自 在两个 Stream 实例之间复制的最佳方法 - C#

这会以 32 KB 块的形式复制流 - 您可能希望减小该块的大小。

The following line in the above sample:

res.Write(reader.ReadToEnd());

Is equivalent to:

string responseString = reader.ReadToEnd();
res.Write(responseString);

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 responseStream = response.GetResponseStream();

byte[] buffer = new byte[32768];
while (true)
{
    int read = responseStream.Read(buffer, 0, buffer.Length);
    if (read <= 0)
        return;
    context.Response.OutputStream.Write(buffer, 0, read);
}

(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.

陌上青苔 2024-10-28 21:19:50

您可以通过将文件直接流式传输到用户的响应流来实现此目的。这样,您就不必等待整个文件下载到服务器上才开始将其发送给用户。

这是代码片段:

public void ProcessRequest(HttpContext context)
{
    string str = "http://download.thinkbroadband.com/100MB.zip"; 

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(str);
    request.Method = "GET";
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        context.Response.ContentType = response.ContentType;
        using (Stream responseStream = response.GetResponseStream())
        {
            responseStream.CopyTo(context.Response.OutputStream);
        }
    }
}

让我知道它是否有效。

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:

public void ProcessRequest(HttpContext context)
{
    string str = "http://download.thinkbroadband.com/100MB.zip"; 

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(str);
    request.Method = "GET";
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        context.Response.ContentType = response.ContentType;
        using (Stream responseStream = response.GetResponseStream())
        {
            responseStream.CopyTo(context.Response.OutputStream);
        }
    }
}

Let me know if it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文