交叉请求无法正确返回文件

本文关键字:返回 文件 请求 | 更新日期: 2023-09-27 18:37:25

我想通过代理脚本向后端服务器发送查询。但它不能正确返回文件。

public class HttpWebRequestRunner : IWebRequestRunner
{
    public HttpWebResponse Run(string backendUri)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(backendUri);
        HttpWebResponse response = (HttpWebResponse) request.GetResponse();
        return response;
    }
}
我的

后端服务器对互联网关闭,所以我向我的 Asp.Net Mvc 应用程序发送参数。并向后端服务器发送请求。

后端服务器正在返回此请求的文件:http://10.0.2.1/Employee/CV/1445

在我的 mvc 控制器中,我使用这个:

 public class PersonController : Controller
    {
        public ActionResult GetCv(int id)
        {
            HttpWebResponse response = new HttpWebResponse();
            HttpWebResponse webResponse = response.run("http://10.0.2.1/Employee/CV/1445");
            context.HttpContext.Response.ContentType = wbResponse.ContentType;
            webResponse.GetResponseStream().CopyTo(context.HttpContext.Response.OutputStream);
            // write result...
        }       
    }

现在

如果我从浏览器向后端发送请求,则此 url http://10.0.2.1/Employee/CV/1445返回 1445.pdf 文件

但是如果我通过这样的prox应用程序发送请求http://localhost:22414/Person/GetCv/1445 这将返回一个名为"文件"的文件,但不返回 PDF 扩展名。

交叉请求无法正确返回文件

文件名位于标头信息中。 webResponse.Headers["Content-Disposition"] .所以你必须像这样使用:

 context.HttpContext.Response.Headers.Set(
     "Content-Disposition", 
     webResponse.Headers.Get("Content-Disposition"));

还需要中继Content-Disposition HTTP 标头。