【问题标题】:Cross request does not return files correctly交叉请求未正确返回文件
【发布时间】:2015-01-13 15:00:44
【问题描述】:

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

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 文件

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

【问题讨论】:

标签: c# asp.net asp.net-mvc httpwebrequest


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 2019-12-04
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      相关资源
      最近更新 更多