使用并行连接创建反向代理

Create Reverse Proxy with parallel connection

我在 C# 中为反向代理编写了这段代码ASP.net

 public class ProxyHandler : IHttpHandler, IRouteHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    DBEntities db = new DBEntities();

    public void ProcessRequest(HttpContext context)
    {
        string inputAction = context.Request.Url.ToString().Substring(context.Request.Url.ToString().IndexOf("/rp/") + 4);
        string action = inputAction.Substring(0, inputAction.IndexOf('/'));
        string input = inputAction.Substring(inputAction.IndexOf('/') + 1);
        if (action == "getFile")
        {
            string hash = input;
            var link = db.LinkDownloaders.Where(tbl => tbl.sourcePathMD5 == input).FirstOrDefault();
            if (link != null)
            {
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(link.sourcePath);
                
                webRequest.Proxy = null;

                webRequest.Credentials = CredentialCache.DefaultCredentials;
               
                if (context.Request.Headers["Range"] != null)
                {
                    string range = context.Request.Headers["Range"].Replace("bytes=", ""); 
                    string[] ranges = range.Split('-');
                    long start = 0;
                    long end = 0;
                    long.TryParse(ranges[0], out start);
                    long.TryParse(ranges[1], out end);
                    if (end > start)
                    {
                        webRequest.AddRange(start, end);
                    }
                    else
                    {
                        webRequest.AddRange("bytes", start);
                    }
                }
                webRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);

                var response = (HttpWebResponse)webRequest.GetResponse();


                string fileName = response.ResponseUri.Segments.LastOrDefault();

                context.Response.AddHeader("content-disposition", "attachment;filename=\""+ fileName +"\"");
                foreach (string item in response.Headers.AllKeys)
                {
                    context.Response.AddHeader(item, response.Headers.Get(item));
                }
                Stream responseStream = response.GetResponseStream();
                context.Response.ContentType = response.ContentType;
                byte[] buffer = new byte[4096];
                while (true)
                {
                    int read = responseStream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return;
                    context.Response.OutputStream.Write(buffer, 0, read);
                }
            }
        }
    }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return this;
    }


}

此代码适用于通过一个连接(浏览器下载器)下载文件,但是当我使用 Internet 下载管理器程序以并行连接方式下载文件或恢复下载时,该代码不起作用。 我对 http headers 了解一点,我为获取文件部分添加了“Range”header,但仍然不起作用。 请给我建议和指导。

用fiddler检查响应和请求头后,我发现部分http请求状态码是“206”而不是“200”,于是我将这行代码添加到我的反向代理处理程序中

context.Response.StatusCode = 206;